/* 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