gpt4 book ai didi

iphone - iOS -- arc 下的 initWith 方法

转载 作者:可可西里 更新时间:2023-11-01 04:19:33 27 4
gpt4 key购买 nike

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com