Use of blocks in objective-C code

Source: Internet
Author: User
 
 
 
 
// 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
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.