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: How Optional Delegate Methods Work

At the beginning of this chapter, you saw that the NSSpeechSynthesizerDelegate protocol defines five optional methods. In your delegate, you implemented only one of them, didFinishSpeaking. A delegate does not have to implement an optional method, but if it does, it will get called. In many languages, this sort of thing would be impossible. How is it achieved in Swift?

NSSpeechSynthesizer is a standard Cocoa class written in Objective-C, but if it were written in Swift the implementation might contain something like this:

c​l​a​s​s​ ​N​S​S​p​e​e​c​h​S​y​n​t​h​e​s​i​z​e​r​ ​{​ ​ ​ ​ ​.​.​.​ ​ ​ ​ ​w​e​a​k​ ​v​a​r​ ​d​e​l​e​g​a​t​e​:​ ​N​S​S​p​e​e​c​h​S​y​n​t​h​e​s​i​z​e​r​D​e​l​e​g​a​t​e​?​ ...

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