Coming from C++, protocols vs categories still mystify me a bit.
They appear to be methods for doing multiple inheritance type stuff without the multiple inheritance problems. From what I understand protocols set a list of methods that a class implementing that protocol must provide, whereas categories let you 'tack-on' methods to existing classes.
They seem like powerful features & I'm sure there is more to them than I've described, but I sometimes have problems re-wiring my brain to 'think in objective-c'.
Seeing more tutorials that show how to use these interesting language concepts in real projects would be very educational.
Protocols are "interfaces" (in the general sense of the word in programming). So they are useful when you want to write a delegate (or callbacks in the general sense of the word) class. Many classes have a delegate property that is of type id<SomeProtocol>. Equivalent to Java's interfaces, but Cocoa use protocols heavily for delegates only.
Categories are useful not for sharing code, but for expanding a class's functionality. This is especially useful when you write a class X that works with an existing class Y, but some of X's code should be an instance method of Y, or X introduces additional capability for Y to support X. UITableView adding functionality to NSIndexPath to support referencing table sections and rows is a good example. All those Java classes that add static methods to support existing Java classes, or if you used Smalltalk and added methods to existing classes, this is the equivalent.
>Equivalent to Java's interfaces, but Cocoa use protocols heavily for delegates only.
Uh, NSCoding, NSCopying and the million other non-delegate protocols? Cocoa uses protocols heavily for a lot of things, not just delegates. The average Cocoa/Cocoa Touch developer, however, uses protocols mostly for delegates.
A protocol says that an object can (and in some (but not all) cases, is required to) implement a given list of methods, leaving the implementation/the "how" up to the programmer. Think of protocols as being similar to interfaces in Java.
OTOH, A category says "in addition to the existing methods, this class will also, for sure, implement these additional methods — and here's how."
They appear to be methods for doing multiple inheritance type stuff without the multiple inheritance problems. From what I understand protocols set a list of methods that a class implementing that protocol must provide, whereas categories let you 'tack-on' methods to existing classes.
They seem like powerful features & I'm sure there is more to them than I've described, but I sometimes have problems re-wiring my brain to 'think in objective-c'.
Seeing more tutorials that show how to use these interesting language concepts in real projects would be very educational.