Simple use of the _ block keyword in IOS

Source: Internet
Author: User

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


 

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.