views:

193

answers:

1

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
+2  A: 

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

Brandon Bodnár
Another way to think about it is subclasses. You use `id` so you you can use the same initializer for a subclass as it's superclass.
Squeegy