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.”)
精彩评论