Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

Blocks

Blocks are a recent extension to the C language. They are not part of the standard ANSI C definition and were added to the language by Apple, Inc. Blocks look and act a lot like functions. The syntax takes some getting used to. You can pass arguments to blocks, just like you can to functions. You can also return a value from a block. Unlike a function, a block can be defined inside a function or method, and gets to access any variables defined outside the block that are within its scope. In general, such variables can be accessed, but their values cannot be changed. There is a special _ _block modifier (that’s two underscore characters that precede the word block) that enables you to modify the value of a variable from inside the block, and you’ll see shortly how that’s used.

Blocks can be passed as arguments to functions and methods, and in Part II, “The Foundation Framework,” you’ll learn about some of the methods that expect to see a block passed as an argument. One of the advantages of blocks is that they can be dispatched by the system for execution by other processors or by other threads within your application.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial