13. Protocols

A protocol is a defined set of methods that a class can choose to implement. Protocols are useful when an object needs to talk to one or more other objects but doesn’t know (or particularly care about) the class of those other objects. Objective-C protocols are the equivalent of Java interfaces. In fact, the designers of Java borrowed the concept from Objective-C. Protocols are used throughout the Cocoa frameworks. Objective-C has both formal and informal protocols. A formal protocol declares a set of required methods and perhaps some optional methods. A class can then adopt the protocol by implementing all the protocol’s required methods. When a class declares that it adopts a formal protocol, the compiler checks to see that the ...

Get Learning Objective-C 2.0: A Hands-on Guide to Objective-C for Mac and iOS Developers, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.