2009-04-10 3 views
1

У меня есть следующий объект:Почему «копировать» не вызывается?

@interface SomeObject : NSObject 
{ 
    NSString *title; 
} 

@property (copy) NSString *title; 

@end 

И у меня есть еще один объект:

@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 

Я не понимаю, почему копия не вызывается, когда я назначаю «название = [pSomeObject название] ». Я всегда думал, что если я установил «copy» в свойстве, он всегда будет вызван. У меня ошибка в коде или я что-то не понимаю?

Заранее спасибо.

ответ

3

Для вызова сеттера вам необходимо использовать цифру .

self.title = [pSomeObject title]; 

или ... использовать точка обозначения для pSomeObject слишком

self.title = pSomeObject.title; 
+0

Спасибо, я не знал разницу. –

Смежные вопросы