Encapsulating Code in Closures

Way back in 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 to this block of code (a Bool indicating if permission was granted, and an NSError optional in case the request for access totally failed) and what will be returned from it (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 on objects) are just a special case of closures!

So what’s so great about ...

Get iOS 9 SDK Development 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.