IOS development-code block usage

Source: Internet
Author: User

/* Print numbers in the Code */nslog (@ "------------------ resultblocks -------------------->"); int (^ resultblocks) (INT) = ^ (INT num) {return num * 20;}; int resultnum = resultblocks (2); nslog (@ "Result: % 4D", resultnum); nslog (@ "-------------- myprintblock ------------------------> "); void (^ myprintblock) (nsstring * X) = ^ (nsstring * Str) {nslog (@ "@ printblock: % @", STR );}; myprintblock (@ "Hello block"); nslog (@ "--------------- P Rintnumblock -----------------------> ");/* separate multiple parameters in code with commas */void (^ printnumblock) (INT, INT); printnumblock = ^ (INT num, int num2) {num = num + num2; nslog (@ "printnum: % d", num) ;}; printnumblock (); nslog (@ "--------------- recursively use -------------------------> "); /** pay attention to the code used for recursion. You must initialize the entire code before calling it. Otherwise, an error will occur! How does one end wrong? 1: Use the sataic keyword to initialize the class before initialization. 2: Use the _ block keyword */_ block void (^ const blocks) (INT) = ^ (int I) {if (I> 0) {nslog (@ "Num: % d", I); blocks (I-1 );}}; blocks (4); static void (^ const blocks2) (INT) = ^ (int I) {if (I> 0) {nslog (@ "Num: % d ", i); blocks2 (I-1) ;}}; blocks2 (4); nslog (@ "--------------- sortarray -----------------------> "); /* sort string arrays in code fast */nsarray * stringarray = [nsarray arraywithobjects: @ "ABC 1", @ "ABC 21", @ "ABC 12 ", @ "ABC 13", @ "ABC 0.5", nil]; nscomparator sortblcok = ^ (ID string1, Id string2) {return [string1 compare: string2];}; nsarray * sortarray = [stringarray Arrays: sortblcok]; nsarray * sortarray2 = [stringarray Arrays: ^ (ID string1, Id string2) {return [string1 compare: string2];}]; nslog (@ "stringarray: % @", stringarray); nslog (@ "sortarray: % @", sortarray2); nslog (@ "------ changeglobalblock ---------------------------> "); /* edit all local variables in code fast */void (^ changeglobalblock) (void) = ^ (void) {Global ++ ;}; changeglobalblock (); nslog (@ "changeglobalblock: % d", global); nslog (@ "------ changlocalnumblock --------------------------->");/* Compilation of local variable changes in the Code is not acceptable, you need to add the _ block keyword before it. Otherwise, the following error will be reported: variable is not assignable (miss_block type specifier */_ block int localnum = 500; void (^ changlocalnumblock) (INT) = ^ (int I) {localnum = localnum + I ;}; changlocalnumblock (30); nslog (@ "changlocalnumblock: % d", localnum );
Refer to this blog:
http://blog.sina.com.cn/s/blog_71715bf8010166ux.html
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.