As a developer, it is especially important to have a learning atmosphere and a communication circle. This is my iOS communication group: 711315161. No matter you are a small white or a big bull, welcome to join us.
1. What is block?
-
Blocks, much like swift closures, are often used for value callbacks, especially in multithreaded network request callbacks. Where closures are functions that can read variables inside other functions.
-
A block begins with a “^”, followed by an argument column enclosed by curly braces, and an action body enclosed by curly braces. There are four types of block: no parameter no return, no parameter return, parameter return, and parameter no return. Generally, block is a parameter block, because the main use of block is to pass parameters.
-
A block is a special OC object that is built on the stack, not the heap, both for performance reasons and for easy access to local variables.
By default, all local variables used by a block are copied, not kept. So it can’t change the value of a local variable.
- If you want to modify an external variable in a block, you can modify the variable directly if it is a static global variable. If it is not, you can use the __block keyword to modify the value of the variable within the block.
2. Block Implementation principle
Objective-c is an extension of the C language, and the implementation of blocks is based on Pointers and function Pointers. From the development of computing language, the earliest GOto, the pointer of high-level language, to the block of object-oriented language, from the thinking of machine, step by step close to the thinking of human, in order to facilitate developers to describe the realistic logic (requirements) more efficiently and directly.
Multithreading and Blocks
GCD and blocks use the dispatch_async series of methods to execute blocks in a specified manner