天天看點

Brief Intro to Blocks 1:Definition

Blocks

In this chapter, you will learn how to program with blocks, a powerful feature available for the Objective-C language. The chapter explores block syntax and semantics, memory management, how to develop blocks in your own code, and how to use blocks in existing APIs (such as the Foundation Framework).

Simply put, blocks provide a way to create a group of statements (i.e., a block of code) and assign those statements to a variable, which can subsequently be invoked. In this way, they are similar to functions and methods, but in addition to executable code, they may also contain variable bindings to stack or heap memory. A block is an implementation of a closure , a function that allows access to variables outside its typical scope. In addition, an Objective-C block is actually an object; it is a subclass of NSObject and has all of its associated properties (you can send messages to it, etc.). Apple developed blocks as an extension to the C family of programming languages (C, Objective-C, and C++). They are available for use with Mac OS X v10.6 and later, as well as iOS 4.0 and later.

Roundup

In this chapter, you learned how to program with blocks, a powerful feature now available for the Objective-C language. It explored block syntax and semantics, memory management, how to develop blocks in your own code, and how to use blocks in existing APIs. The following are the key takeaways from this chapter:

  • Blocks are similar to functions and methods, but in addition to executable code, they may also contain variable bindings to stack or heap memory. A block is an implementation of a closure, a function that allows access to variables outside its typical scope.
  • Blocks can be declared as variables of block type, and block variables can be used anywhere standard variables can—as parameters to a function/method, and so forth.
  • A block literal expression contains the code invoked by a block. It can be defined inline, as an anonymous function, and thereby serve as the parameter for a function/method invocation.
  • A block augments the visibility of variables compared to C functions through lexical scoping, __block variables, instance variable access, and const imports.
  • At runtime, a block literal expression is allocated on the stack and thus has the same lifetime as a local variable. As a result, it must be copied to permanent storage (i.e., onto the heap) to be used outside of the scope in which it is defined.
  • The Block_copy() and Block_release() operations are used to copy/release blocks on the heap when using MRR memory management. With ARC memory management, the compiler automatically performs the block copy and release operations as long as the block does not return an id type or pass an id type as a parameter.
  • Blocks are most naturally used to implement small, self-contained pieces of code that encapsulate units of work. They are typically executed concurrently over the items of a collection or as a callback when an operation has finished. Because blocks can be defined inline, they can eliminate the need to create entirely separate classes and methods for context-sensitive code, such as asynchronous completion handlers. They also enable related code to be kept together, and not scattered among multiple files.

This was a detailed chapter that exposed you to a variety of advanced topics: blocks, concurrent programming, and the GCD APIs. Spend a little time to go over everything covered here. Feel free to modify the examples to learn more about these concepts. In the next chapter, you will learn all about Objective-C literals, another recent addition to the Objective-C language.

繼續閱讀