// Int code block int Mutl = 7; int (^ visenblock) (INT) = ^ (INT num) {return num * Mutl ;}; nslog (@ "% d ", visenblock (4); // nsstring code block void (^ visenprintstring) (nsstring * Str) = ^ (nsstring * Str) {nslog (@ "Print: % @", str) ;}; visenprintstring (@ "aaaa"); // The code is used to sort nsarray * stringarray = [nsarray arraywithobjects: @ "ABC 1", @ "ABC 21 ", @ "ABC 12", @ "ABC 13", @ "ABC 05", nil]; nscomparator visensortblock = ^ (ID string1, Id string2) {return [string1 compare: string2] ;}; nsarray * sortarray = [stringarray sortedarrayusingcomparator: visensortblock]; nslog (@ "sortarray: % @", sortarray ); // recursive call of a code block // The code block must be a global or static variable to be called recursively. In this way, the code block variable is initialized when the program starts, you can call static void recursively (^ const visenrecursionblock) (INT) = ^ (int I) {if (I> 0) {nslog (@ "Num: % d", I ); visenrecursionblock (I-1) ;};}; visenrecursionblock (3); // use the global variable void (^ visenvariablesblock) (void) = ^ (void) {ivisen + +; nslog (@ "% d", ivisen) ;}; visenvariablesblock ();
Reprinted from: http://www.xue5.com/Mobile/iOS/658965.html