Objective-C Block exercises

Source: Internet
Author: User


Int ivisen = 1000;-(void) viewdidload {[Super viewdidload]; // do any additional setup after loading the view, typically from a nib. // 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 in string array sorting nsarray * stringarra Y = [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 visibility: visensortblock]; nslog (@ "sortarray: % @ ", sortarray); // recursive call of the code block // The code block must be a global or static variable, in this way, when the program starts, the code block variable is initialized. You can recursively call static void (^ const visenrecursionb Lock) (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 (); // use local variables in the code block // change local variables in the code block. Compilation fails. How can I change local variables in a code block? Add the keyword __block _ block int ivisena = 1000 before the local variable; void (^ visenvariablesblockb) (void) = ^ (void) {ivisena ++; nslog (@ "Local: % d", ivisena) ;}; visenvariablesblockb ();}

 

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.