Objective-C Block
Block Code block is a feature of OC. In addition to executable code, it may also contain automatic variable binding (stack) or memory hosting (HEAP ). Therefore, a block maintains a state set (data) and can be executed at any time. Block is particularly useful for callback.
Block can be used as the return value of a function parameter or function, and can include an input parameter or return value. It is widely used in multiple threads, asynchronous tasks, set traversal, and interface callback. Use the identifier ^ .. For the sake of performance, the block is allocated to the stack, and its scope is the current function.
Let's talk about the similarity between block and function:
(1) code can be saved;
(2) return values;
(3) tangible parameters;
(4) The call method is the same;
The following code demonstrates the block usage:
# Import
// Declare a closure; // No return value, no input parameter; void (^ one) (void); // return value, no input parameter; int (^ two) (void); // No return value; input parameter; void (^ three) (int); // return value, input parameter; int (^ four) (int, int); int main (int argc, const char * argv []) {@ autoreleasepool {// defines the closure function; one = ^ (void) {NSLog (@ executed one) ;}; two =^ (void) {NSLog (@ executed two); return 2 ;}; three = ^ (int) {NSLog (@ executed three) ;}; four = ^ (int a, int B) {NSLog (@ executed four); return a + B ;}; // call the closure function; one (); two (); three (1); four (2, 3); NSLog (@ % d, two ()); NSLog (@ % d, four (2, 3);} return 0 ;}
The output result is as follows:
.
Similarly, we can directly define the block when declaring it. The example is as follows:
# Import
Int main (int argc, const char * argv []) {@ autoreleasepool {// I can also define it directly when declaring it here; void (^ block) (int) = ^ (int a) {NSLog (@ % d, a) ;}; block (3);} return 0 ;}
The output result is as follows:
.
A very important function of block is to capture values. Take a look at the sample code: At this time, we can only capture the value of num and cannot modify it, because this is a value type and it is passed through the value.
# Import
Int main (int argc, const char * argv []) {@ autoreleasepool {// Block Value capture int num = 10; void (^ myBlock) (void) = ^ (void) {// we can access external variables within the block, but cannot modify them; NSLog (@ num = % d, num) ;}; myBlock () ;}return 0 ;}
The output result is as follows:
.
If you need to modify the value of the external variable within the block because of the actual needs of the project, you can use the _ block keyword to modify the variable. The sample code is as follows: in this case, the variable becomes the reference type. You can modify the value of a block.
However, please note that as long as the block is not copied, the block is always in the stack. To extend the block scope, you can perform the copy operation. The interface provided by apple is the Block_Copy () method, copy the block to heap.
# Import
Int main (int argc, const char * argv []) {@ autoreleasepool {// Block Value capture _ block int num = 10; void (^ myBlock) (void) = ^ (void) {// we can access external variables within the block, but cannot modify them; NSLog (@ num = % d, num); num = 20; // modify the value of the external variable;}; myBlock (); NSLog (@ numnum = % d, num);} return 0 ;}
The output result is as follows:
.