In Objective C, a category to NSObject without an implementation. (Please see my writeup under id.)

All you have to do is create a category consisting of only method prototypes, with no actual implementations of those methods defined. The compiler will then believe that every single object in existence supports those methods, although in reality only those classes that have explicitly implemented the method will. (If you speak C++, you could think of this as adding a virtual function to every class in existence simultaneously.)

The upshot is that you don't have to look at all those "NSObject does not support method 'metasyntacticVariable'" messages. Meaning your program can assume any given id supports the method you are calling, without caring about types or protocols-- and if it doesn't, well, you'll just get nil back.

It would be more structured, more "safe" and thus really much better if you were to do this same thing by using a formal protocol, but sometimes formal protocols can be cumbersome or impractical. The only real advantage to an informal protocol over a formal one is that with an informal protocol, the conforming object does not have to implement all of the methods, whereas a class that states itself as conforming to a formal protocol must implement all the methods declared. If this would cause problems, informal protocols would probably be the way to go.

Log in or register to write something here or to contact authors.