2009-04-10 334 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標題] 」。我總是想,如果我在屬性中設置「複製」它總是會被調用。我有一個錯誤在我的代碼或我不明白的東西嗎?

預先感謝您。

回答

3

對於二傳手得到叫你需要使用符號。

self.title = [pSomeObject title]; 

或...使用記法pSomeObject太

self.title = pSomeObject.title; 
+0

謝謝你,我不知道其中的差別。 – 2009-04-11 06:36:27

相關問題