过去,我们受过这样的训练来编写 init 方法:
Foo.h:
-(id) initWithInteger: (NSInteger) someNumber;
Foo.m:
-(id) initWithInteger: (NSInteger) someNumber {
if ((self = [super init])) {
doSomeStuff;
}
return self;
}
但是那些 id 转换现在是不行的。那么这些方法现在应该是什么样子呢?
编辑:这是一个图书馆。所以不知道调用代码可能是什么样子。
最佳答案
为什么说返回 id
是不行的? ARC specification说
init
methods must be instance methods and must return an Objective-C pointer type. Additionally, a program is ill-formed if it declares or contains a call to an init method whose return type is neither id nor a pointer to a super-class or sub-class of the declaring class (if the method was declared on a class) or the static receiver type of the call (if it was declared on a protocol).
此外,请记住使用 ARC
是针对每个文件的决定,即使用 ARC 编译的代码可以与不使用 ARC 的代码一起使用,反之亦然。在库的头文件的情况下,您应该准备它以便它可以在两者上使用,但为非 ARC 情况准备就足够了。
关于iphone - iOS -- arc 下的 initWith 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6873433/