For the More Curious: Creating a Protocol

Creating your own protocol is very simple. Here is a protocol with two methods.

p​r​o​t​o​c​o​l​ ​E​n​c​r​y​p​t​a​b​l​e​ ​{​
 ​ ​ ​ ​i​n​i​t​(​e​n​c​r​y​p​t​e​d​D​a​t​a​:​ ​N​S​D​a​t​a​)​
 ​ ​ ​ ​f​u​n​c​ ​e​n​c​r​y​p​t​(​)​ ​-​>​ ​N​S​D​a​t​a​
}​

The protocol would typically be defined in a file called Encryptable.swift:

If your protocol is tagged @objc, it is possible to add optional methods.

@​o​b​j​c​ ​p​r​o​t​o​c​o​l​ ​E​n​c​r​y​p​t​a​b​l​e​ ​{​
 ​ ​ ​ ​i​n​i​t​(​e​n​c​r​y​p​t​e​d​D​a​t​a​:​ ​N​S​D​a​t​a​)​
 ​ ​ ​ ​f​u​n​c​ ​e​n​c​r​y​p​t​(​)​ ​-​>​ ​N​S​D​a​t​a​

 ​ ​ ​ ​o​p​t​i​o​n​a​l​ ​f​u​n​c​ ​s​c​h​e​m​e​N​a​m​e​(​)​ ​-​>​ ​S​t​r​i​n​g​
}​

In this example, encryptData(_:) and ...

Get Cocoa Programming for OS X: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.