Wednesday, July 20, 2011

Xcode iOS, Objective C: What is Protocol?

A protocol is means to define a list of required and/or optional methods that a class implements. If a class adopts a protocol, it must implement all required methods in the protocols it adopts.

Cocoa uses protocols to support interprocess communication through Objective-C messages. As Objective-C does not support multiple inheritance, you can achieve similar functionality with protocols, as a class can adopt more than one protocol.

An Example of a protocol is NSCoding, which has two required methods that a class must implement.
This protocol is used to enable classes to be encoded and decoded, that is, archiving of objects by writing to permanent storage.

Another example is when using a tableview, your class implements thecellForRowAtIndexPath method which asks for cell content to insert into a table – thecellForRowAtIndexPath method is defined within the UITableViewDataSource protocol.

No comments:

Post a Comment

Kamleshwar