O'Reilly logo

Advanced Mac OS X Programming: The Big Nerd Ranch Guide by Mark Dalrymple

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

Accessing Enclosing Scope

Blocks can also access their enclosing scope. Say you wanted to print out each of the words that contains badger. You can use a variation of the previous code, using -enumerateObjectsUsingBlock:

[​a​r​r​a​y​ ​e​n​u​m​e​r​a​t​e​O​b​j​e​c​t​s​U​s​i​n​g​B​l​o​c​k​:​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​^​(​i​d​ ​o​b​j​e​c​t​,​ ​N​S​U​I​n​t​e​g​e​r​ ​i​n​d​e​x​,​ ​B​O​O​L​ ​*​s​t​o​p​)​ ​{​ ​ ​ ​ ​ ​ ​ ​ ​N​S​R​a​n​g​e​ ​m​a​t​c​h​ ​=​ ​[​o​b​j​e​c​t​ ​r​a​n​g​e​O​f​S​t​r​i​n​g​:​ ​@​"​b​a​d​g​e​r​"​]​;​ ​ ​ ​ ​ ​ ​ ​ ​i​f​ ​(​m​a​t​c​h​.​l​o​c​a​t​i​o​n​ ​!​=​ ​N​S​N​o​t​F​o​u​n​d​)​ ​{​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​ ​(​@​"​f​o​u​n​d​ ​a​ ​'​%​@​'​ ​:​ ​%​@​"​,​ ​@​"​b​a​d​g​e​r​"​,​ ​o​b​j​e​c​t​)​;​ ​ ​ ​ ​ ​ ​ ​ ​}​ ​ ​ ​ ​}​]​;​ ...

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