Chapter 6Waiting for Things to Happen with Closures

It’s very tempting to think of our code as a series of instructions, to be executed in order. But this falls down when any of these steps takes a long time, or worse yet, an unknown amount of time.

Imagine that instead of programming an iPhone, we’re 50 years in the future, programming a household robot to do ordinary household tasks. Let’s say we want to write a program to answer the phone (OK, and imagine there are still phones 50 years from now). We might write something like:

 
Pick up the phone.
 
Say "hello".
 
Wait for the other party to introduce themselves.
 
If they're a family member, let us know.
 
If they're a politician or an advertiser, hang up.
 
Otherwise, ask us what to do.

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.