在编写 Objective-C 类时,我使用以下便捷方法结构:
+ (MyClass *) myClass {
return [[[self alloc] init] autorelease];
}
- (id) init {
if (self = [super init]) {
// set-up code here...
}
return self;
}
MyClass*
便利方法应该指定返回类型而不是有什么理由id
?或者该init
方法应该指定任一返回类型?
这似乎是 Objective-C 代码中的常见模式。直到现在才真正考虑过。