Simple use of the _ block keyword in IOS
/** 13. _ When is block used? **/
Conclusion: To modify the value of a local variable in a block, use _ block to modify the value.
/** 14. Ask a question: in the block, add an array. Do I need to declare the array as _ block? **/
It does not need to be declared as _ block, because the pointer to the testArr array has not changed (adding objects to the array does not change, but the content in the memory to which the Pointer Points has changed)
/** 15. In the block, modify the NSInteger. Does this NSInteger need to be declared as _ blcok? **/
If the NSInteger value changes, _ block must be added.
Code example:
NSMutableArray * testArr = [[NSMutableArray alloc] initWithObjects: @ "1", @ "2", nil]; _ block NSInteger a = 10;/** conclusion: modify the value of a local variable in the block with _ block. **/void (^ TestBlock) (void) = ^ {// NSMutableArray * temArr = [[NSMutableArray alloc] init]; // testArr = temArr; // when the pointer of the testArr array changes, to add _ block to testArr to modify a = 100; // if the value of a is changed, _ block must be added. // testArr does not need to declare _ block, because the pointer to the testArr array has not changed (adding objects to the array does not change, but the content in the memory pointed to by the pointer has changed) [testArr addObject: [NSString stringWithFormat: @ "3"]; NSLog (@ "_ block testArr: % @ a: % d", testArr, a) ;}; a = 0; TestBlock (); NSLog (@ "testArr: % @ a: % d", testArr, );
Running result:
20:16:23. 862 WXMovie_study [22827: 1683307] _ block testArr :(
1,
2,
3
) A: 100
20:16:23. 862 WXMovie_study [22827: 1683307] testArr :(
1,
2,
3
) A: 100