If you've done any kind of object-oriented programming, you probably know protocols as interfaces (they're not identical, but the concept is similar). If not, think of protocols as blueprints.
The main reason why you'd use protocols is so you can use objects without knowing everything about them; all you need to know is that they implement a set of methods. For example, if the classes Business
and Person
conform to the protocol Contact
, which defines the method - (NSString *)phoneNumber
, the class AddressBook
can call -(NSString *)phoneNumber
without knowing whether or not the object is of type Business
or Person
.
Once you start to learn about Cocoa and delegates, you'll see how powerful and important protocols are.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…