Encapsulating Code in Closures

Way back in Swift Methods, we mentioned that functions use the syntax -> to indicate the type of their return value. So, is that (Bool, NSError!) -> Void stuff a function? Actually yes…kind of! What this syntax expresses is a closure, a self-contained block of functionality. The syntax indicates what will be passed in (a Bool indicating if permission was granted, and an NSError optional in case the request for access totally failed) and what will be returned (Void, that is to say, there is no return value). So it’s not that this parameter is a function; it’s a closure…and in Swift, all functions (and thus all methods) are just a special case of closures!

So what’s so great about closures? Well, for one thing, ...

Get iOS 8 SDK Development, 2nd Edition 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.