O'Reilly logo

Cocoa Programming for OS X: The Big Nerd Ranch Guide by Aaron Hillegass, Adam Preble, Nate Chandler

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

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 ...

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