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

Exercises

  1. Rewrite this function as a block:

    i​n​t​ ​k​(​v​o​i​d​)​ ​{​
     ​ ​ ​ ​r​e​t​u​r​n​ ​1​;​
    }​
    

  2. Write a function create_k that accepts a single integer argument i and returns a block. The returned block accepts no arguments and always returns the value of i passed to create_k(). Use this function in a test program. (Be careful with the block memory management!)

  3. Define two blocks called add_one and add_two that accept no arguments and return nothing. These should both increment the same integer by one or two. (You will need the __block storage specifier.)

  4. Use typedefs to simplify the following block reference declaration:

    i​n​t​ ​(​^​(​*​(​^​g​e​t​_​b​l​o​c​k​_​f​a​c​t​o​r​y​_​f​u​n​c​p​t​r​)​(​v​o​i​d​)​)​(​i​n​t​)​)​(​v​o​i​d​)​;​  ...

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