O'Reilly logo

Application Development with Swift 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

Protocols

Protocols are one of the most important and commonly used methodologies in programming in iOS and OS X. In protocols, you can define a set of methods and properties that will be implemented by classes that will conform to these protocols. In protocols, you just describe things without any implementations. In Swift, classes are not the only ones that can conform to protocols, but also structures and enumerations can conform to protocols as well.

To create a protocol, just use the protocol keyword and then its name:

protocol SampleProtocol
{
    
}

Then, when types are going to conform to this protocol, add a colon (:) after the name of type and list the protocols separated by commas (,). Check the following example:

class SampleClass: SampleProtocol, ...

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