2009-04-10 6 views
1

je l'objet suivant:Pourquoi "copier" n'est pas invoqué?

@interface SomeObject : NSObject 
{ 
    NSString *title; 
} 

@property (copy) NSString *title; 

@end 

Et j'ai un autre objet:

@interface AnotherObject : NSObject{ 
     NSString *title; 
    } 

    @property (copy) NSString *title; 

    - (AnotherObject*)init; 
    - (void)dealloc; 
    - (void) initWithSomeObject: (SomeObject*) pSomeObject; 
    + (AnotherObject*) AnotherObjectWithSomeObject (SomeObject*) pSomeObject; 

    @end 

@implementation AnotherObject 
@synthesize title 


    - (AnotherObject*)init { 
     if (self = [super init]) { 
      title = nil; 
     } 
     return self; 
    } 

    - (void)dealloc { 
     if (title) [title release]; 
     [super dealloc]; 
    } 

    -(void) initWithSomeObject: (SomeObject*) pSomeObject 
    { 
     title = [pSomeObject title]; //Here copy is not being invoked, have to use [ [pSomeObject title] copy] 
    } 


    + (AnotherObject*) AnotherObjectWithSomeObject (SomeObject*) pSomeObject; 
    { 
     [pSomeObject retain]; 
     AnotherObject *tempAnotherObject = [ [AnotherObject alloc] init]; 
     [tempAnotherObject initWithSomeObject: pSomeObject]; 
     [pSomeObject release]; 
     return tempAnotherObject; 
    } 

@end 

Je ne comprends pas, pourquoi la copie n'est pas invoqué lorsque j'assigne « title = [title pSomeObject] ". J'ai toujours pensé que si je mettais "copier" dans la propriété, il serait toujours invoqué. J'ai une erreur dans mon code ou je ne comprends pas quelque chose?

Merci à l'avance.

Répondre

3

Pour qu'un setter soit appelé, vous devez utiliser la notation dot.

self.title = [pSomeObject title]; 

ou ... à utiliser la notation pointpour pSomeObject trop

self.title = pSomeObject.title; 
+0

Merci, je ne savais pas la différence. –

Questions connexes