O'Reilly logo

Learning iOS Development: A Hands-on Guide to the Fundamentals of iOS Programming by Erica Sadun, Rod Strougo, Maurice Sharp

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

Variable Scope

In the taxi pulsing example, you might have noticed something strange about the animation block. It uses pulseAmount, spaceAmount, and self. But how? If this were a function, each of those would need to be an argument. But the block takes no arguments.

This is one of the very powerful features of blocks. They have access to all the variables at the point where they were created. That is, they pull in all the variables from the current scope. One way to think about this is that they have access to the same variables that a line of code in exactly the same position would have. If you have used languages like Smalltalk and Lisp, you can think of blocks as closures.

Try the following example by adding the code to viewDidLoad for any ...

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