关于客观c属性设置器如何工作的问题

时间:2011-02-21 20:33:21

标签: iphone objective-c properties

我对属性的两个假设是否正确?

@interface Foo : NSObject {
NSDate *Created;
}

@property (nonatomic, retain) NSDate *Created;

@end

@implementation Foo

@synthesize Created;

- (id) init {
if(self = [super init])
{
    Created = [NSDate date]; //this will not call the setter and instead just access the variable directly, which means it will not automatically get retained for me.
    self.Created = [NSDate date]; // this will call the setter, which will retain the variable automatically for me.
}
return self;
}

- (void)dealloc {
    [Created release]   
    [super dealloc];
}
@end

2 个答案:

答案 0 :(得分:7)

是;这是正确的。

请注意,实例变量应为created;它应该以小写字母开头。我还建议creationDate

答案 1 :(得分:0)

建议不要在dealloc或init中使用属性,因此在init方法中而不是执行self.propertyName = [NSDate date];,你应该执行fieldName = [[NSDate alloc] init]; dealloc中的发布很好但是

More about it

相关问题