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: Functional Methods and Minimizing Closure Syntax

Although it is not a functional language, Swift has a number of features to support functional programming. In particular, the standard library includes the functions map, reduce, and filter. A full overview of these functions is beyond the scope of this book, but they can be very helpful in constructing clear code for common tasks.

Recall that the Employee class has a property, name, whose type is String?. Here’s how you might use these methods to create an array of all non-nil names:

l​e​t​ ​n​a​m​e​s​ ​=​ ​e​m​p​l​o​y​e​e​s​.​f​i​l​t​e​r​(​{​ ​e​m​p​l​o​y​e​e​ ​i​n​ ​ ​ ​ ​r​e​t​u​r​n​ ​e​m​p​l​o​y​e​e​.​n​a​m​e​ ​!​=​ ​n​i​l​ }​)​.​m​a​p​(​{​ ​e​m​p​l​o​y​e​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