2009-04-10 20 views
1

Ich habe folgendes Objekt:Warum wird "Kopieren" nicht aufgerufen?

@interface SomeObject : NSObject 
{ 
    NSString *title; 
} 

@property (copy) NSString *title; 

@end 

Und ich habe ein anderes Objekt:

@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 

Ich verstehe nicht, warum Kopie nicht, wenn ich „title = [pSomeObject title] Zuordnung bin aufgerufen wird ". Ich dachte immer, wenn ich "Kopie" in der Eigenschaft setze, wird es immer aufgerufen. Ich habe einen Fehler in meinem Code oder ich verstehe etwas nicht?

Vielen Dank im Voraus.

Antwort

3

Damit ein Setter aufgerufen werden kann, müssen Sie die Punkt Notation verwenden.

self.title = [pSomeObject title]; 

oder ... Punkt Notation für pSomeObject verwenden zu

self.title = pSomeObject.title; 
+0

Danke, habe ich den Unterschied nicht kennen. –

Verwandte Themen