O'Reilly logo

iOS Programming Cookbook by Hossam Ghareeb

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Working with protocols and delegates

Protocol is a set of methods and properties for a particular task to which classes, structure, or enumeration can be conformed.

Getting ready

The syntax of protocol goes like this:

protocol ProtocolName{ 
   // List of properties and methods goes here.... 
} 

The keyword protocol followed by the protocol name and curly braces are the building blocks of any protocol you need to write. Classes, structures, or enumeration can then conform to it like this:

class SampleClass: ProtocolName{ 
} 

After class name, you type colon and the super class name that this class extend from if any, followed by a list of protocols that you want to conform to with a comma separation.

How to do it...

  1. Create a new playground file in Xcode called ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required