【发布时间】:2010-11-03 19:53:22
【问题描述】:
这是上一个问题的一个分支,这是不好的做法(使用属性设置 iVars)吗?
// Designated initializer 001
- (id)initWithName:(NSString *)newName andType:(NSString *)newType {
self = [super init];
if(self) {
[self setName:newName];
[self setType:newType];
}
return self;
}
或者我应该使用...
// Designated initializer 002
- (id)initWithName:(NSString *)newName andType:(NSString *)newType {
self = [super init];
if(self) {
name = [newName retain];
type = [newType retain];
}
return self;
}
我一直在使用 001 版,但被引导相信在 init 或 dealloc 中使用属性访问 iVar 是不好的做法。
编辑:将retain添加到版本002
加里。
【问题讨论】:
标签: iphone objective-c cocoa-touch