1 What is block
There are many definitions for closures (blocks), where closures are functions that can read variables inside other functions, which are close to nature and better understood. For the first contact with the block of the classmate, will feel a bit around, because we are accustomed to write such a program main () {Funa ();} Funa () {Funb ();} Funb () {...}; Is the function main called function A, function A called function B ... Functions are executed sequentially, but not in reality, such as project manager M, who has 3 programmers a, B, C, when he gives programmer a to implement functional F1, he does not wait for a to complete, then to arrange B to implement F2, but to arrange for a function F1,b function F2,c function F3, Then maybe write a technical document, and when a has a problem, he will come to the project manager m, when B is done, will notify M, this is an example of asynchronous execution. In this case, block will be able to work, because in the project manager m, to a job, and will also tell a if encounter difficulties, how can find him to report problems (such as hit his mobile phone number), this is the project manager m to a callback interface, to return the operation, such as receiving telephone, Baidu query, Return Web page content to a, this is a block, in M confessed to work, has been defined, and obtained the F1 task number (local variable), but when a encountered a problem, only call execution, cross-function in the project manager m query Baidu, get results back to the block. 2 Block Implementation Principle objective-c is the extension of the C language, the implementation of block is based on pointers and function pointers. From the development of computational language, the earliest goto, the pointer to the high-level language, to the object-oriented language block, from the machine's thinking, a step closer to human thinking, in order to facilitate developers more efficient, direct description of the logic of Reality (demand). Here are two good introductions to block implementation of block implementation in iOS talk about the implementation of Objective-c block 3 block using instance Cocoatouch frame animation effect Use typed to declare blocktypedef void (^didfinishblock) (NSObject *ob); This declares a block of type didfinishblock, and then it is available @property ( nonatomic,copy) didfinishblock Finishblock; Declare a block object, notice that the object property is set to copy and a copy is automatically copied when the block parameter is received. __block is a special type, the local variable declared with the keyword can be changed by the block, and its value in the original function will be changed. 4 Common series of questions interview, the interviewer will ask some, whether to understand the block, whether the use of blocks, these questions are equivalent to the opening, often the following a series of problems began, so be sure to truthfully according to their own situation to answer. 1 What are the advantages of using block and using delegate to complete the delegation mode? The first thing to understand is the delegate mode, which is used extensively in iOS, which is an object adapter in the adapter pattern in design mode, and OBJECTIVE-C uses the ID type to point to everything, making the delegate mode more concise. Understand the details of the delegate pattern: ios design mode----Delegate Mode use block to implement the delegate mode, the advantage is that the callback block code block is defined inside the delegate object function, so that the code is more compact, the adaptation object no longer need to implement a specific protocol, The code is more concise.   2 multi-Threading with BLOCKGCD and block using the dispatch_async series method, the full definition of BLOCKGCD programming instance dispatch_async can be executed in the specified manner void Dispatch_async ( dispatch_queue_t queue, dispatch_block_t block); Function: Commits an asynchronously executed block in the specified queue, does not block the current thread through queue to control the thread that the block executes. The main thread executes the Finishblock object defined in the previous article Dispatch_async (Dispatch_get_main_queue (), ^ (void) {Finishblock ();});
Use of the iOS syntax---block