When you declare a type variable
id<MyProtocol> var;
the Objective-C compiler only knows about the methods in MyProtocol and thus will warn you if you try to call any of the NSObject methods, such as -retain/-release , in this instance. Thus, Cocoa defines the NSObject protocol, which reflects the methods of the NSObject class and instance. MyProtocol announcing that MyProtocol implements the NSObject protocol, you give the compiler a hint that all NSObject methods will be implemented by an instance that implements MyProtocol .
Why is all this necessary? Objective-C allows objects to descend from any root class. In Cocoa, NSObject is the most common, but not the only root class. NSProxy also the root class. Therefore, an instance of type id does not necessarily inherit NSObject methods.
Barry Wark Mar 25 '09 at 17:45 2009-03-25 17:45
source share