gpt4 book ai didi

iPhone init 方法返回类型

转载 作者:行者123 更新时间:2023-12-03 20:59:04 24 4
gpt4 key购买 nike

假设我们正在 iPhone 程序中编写一个类(我们称之为 Class)。在所有示例中,init 方法通常是这样声明的:

-(id) initWithFoo: (Foo *) foo

我的问题是:执行以下操作会更合乎逻辑吗?为什么或为什么不?

-(Class *) initWithFoo: (Foo *) foo

最佳答案

来自Implementing an Initializer在 Mac 开发中心

The reason for this is that id gives an indication that the class is purposefully not considered—that the class is unspecified and subject to change, depending on context of invocation. For example, NSString provides a method initWithFormat:. When sent to an instance of NSMutableString (a subclass of NSString), however, the message returns an instance of NSMutableString, not NSString. (See also, though, the singleton example given in “Combining Allocation and Initialization.”)

关于iPhone init 方法返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2480996/

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