Book Image

Application Development with Swift

By : Hossam Ghareeb
Book Image

Application Development with Swift

By: Hossam Ghareeb

Overview of this book

Table of Contents (14 chapters)

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, AnotherProtocol {
    
}

Now, let's talk about protocol properties and methods.

Properties

When you list properties between {} in protocol, you specify that these properties are required to exist in types that conform to this protocol. You...