开发者

iPhone init method return type

Suppose we are writing a class (let's call it Class) in an iPhone program. In all the samples out there, the init methods are typically declared like this:

-(id) initWithFoo: (Foo *) foo

My 开发者_开发问答question is: would it be more logical to do the following? Why or why not?

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


From Implementing an Initializer on Mac Dev Center

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.”)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜