concept
The code block block is an extension to the C language introduced by Apple in iOS4 to implement the characteristics of anonymous functions. Block is a special data type that can define variables normally, as parameters, and as return values. In particular, block You can also save a piece of code and call it when needed. At present, block has been widely used in iOS development, often used for GCD, animation, sorting and various callbacks.
block: We call it a code block, which is similar to a method. And each method goes from the hard disk to the memory when it is called, and then executes it, and then disappears after the execution. Therefore, the memory of the method does not need to be managed by us, that is, the method is in the stack area of the memory. Therefore, the block is not like the class object in OC (in the heap area), it is also in the stack area. If we use block as an attribute of an object, we will use the keyword copy to modify it, because it is in the stack area and we cannot control its demise. When we modify it with copy, the system will copy the implementation of the block. Share it to the heap area, so that our corresponding attribute will have the ownership of the block. It can be guaranteed that the block code block will not die prematurely.
Definition and use
Declaration of block variable
The declaration format of the block variable: return value type (^block name) (parameter list);
1 2 3 4 5 |
|
Assignment of block variables
1 2 3 4 5 6 |
|
Assign value while declaring block variable
1 2 3 4 5 |
|
Use typedef to define the block type
In the actual use of Block, we may need to repeatedly declare multiple Block variables with the same return value and the same parameter list. It will be very cumbersome to write a long series of codes repeatedly to declare variables, so we can use typedef to define Block type
1 2 3 4 5 6 7 |
|
block as a function parameter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Access local variables in the block
- Local variables can be accessed in the block
- Modify the local variables after the Block is declared and before the Block is called. When the Block is called, the value of the local variable is the old value before the modification.
- Local variables cannot be modified directly in the block
1 2 3 4 5 6 7 8 |
|
- Use the underscore and underscore block modification before the local variables, modify the local variables after the block is declared and before the block is called. When the block is called, the value of the local variable is the new value after the modification.
- Use the underscore underline block modification before the local variables, and the local variables can be directly modified in the block
1 2 3 4 5 6 7 8 9 10 |
|
Access global variables in the block
Modify the global variables after the block is declared and before the block is called. When the block is called, the value of the global variable is the new value after the modification.
1 2 3 4 5 6 7 8 9 10 |
|
Access static variables in the block
1 2 3 4 5 6 7 8 9 |
|
Circular reference problem caused by block
If there is a Block property inside the object, and the object is accessed inside the Block, it will cause a circular reference
Situation One
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
Situation two
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
The way to solve the circular reference is to use a weak reference pointer to point to the object, and then use the weak reference pointer inside the block to operate, which avoids the block from making strong references to the object
Situation One
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
Situation two
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|