为什么没有调用“复制”?

时间:2009-04-10 19:28:35

标签: objective-c

我有以下对象:

@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

我不明白,为什么在分配“title = [pSomeObject title]”时没有调用copy。我总是认为如果我在属性中设置“复制”,它总是会被调用。我的代码中有错误或者我不明白?

提前谢谢。

1 个答案:

答案 0 :(得分:3)

要调用setter,您需要使用 dot 表示法。

self.title = [pSomeObject title];

或...也为pSomeObject使用 dot 表示法

self.title = pSomeObject.title;