我已經閱讀了大量摘錄,提到你不應該在你的init或dealloc方法中使用點符號。但是,我似乎無法找出原因。有一篇文章順帶提到它與KVO有關,但沒有更多。Objective-C點語法和初始化
@interface MyClass : NSObject {
SomeObject *object_;
}
@property (nonatomic, retain) SomeObject *object;
@end
這個實現不好嗎?
@implementation MyClass
@synthesize object = object_;
- (id)initWithObject:(SomeObject *)object {
if (self = [super init]) {
self.object = object;
}
return self;
}
@end
但是這樣好嗎?
@implementation MyClass
@synthesize object = object_;
- (id)initWithObject:(SomeObject *)object {
if (self = [super init]) {
object_ = [object retain];
}
return self;
}
@end
在init中使用點符號的缺陷是什麼?
看來在第二個例子中你應該寫object_ = [object retain]; – Vladimir 2010-06-30 15:13:24
固定,用快速書寫的例子麻煩。 ;) – MarkPowell 2010-06-30 15:17:54