iphone - iOS -- arc 下的 initWith 方法

标签 iphone objective-c ios automatic-ref-counting

过去,我们受过这样的训练来编写 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/

相关文章:

objective-c - NSBundle pathForResource 为 NULL

ios - 为什么不能在 didSelectRowAtIndexPath 方法中关闭 View Controller

iOS 方向改变,但页面仍然垂直

ios - Facebook IOS SDK : how to do silent sign in?

iphone - 如何通过代码获取iPhone当前位置?

iphone - UIScrollView 在模拟器中过来 UIImage

objective-c - 如何找出 Xcode 中首先运行的文件

javascript - 是否可以通过 JavaScript/jQuery 阻止 iPhone 网站中的 "flipping"?

iphone - 有没有办法在我的应用程序中不使用 CLLocationManager 委托(delegate)来获取当前位置

ios - 请求 HealthKit 权限时对 UIViewController 的不平衡调用