IOS development-code block usage

Source: Internet
Author: User

[Java] <P style = "TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 26px; BORDER-RIGHT-WIDTH: 0px; LIST-STYLE-TYPE: none; MARGIN-TOP: 0px; FONT-FAMILY: Arial; WORD-WRAP: normal; MARGIN-BOTTOM: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; COLOR: rgb (, 73); FONT-SIZE: 14px; WORD-BREAK: normal; BORDER-LEFT-WIDTH: 0px; PADDING-TOP: 0px "> </P> <PRE class = plain name =" code "> </PRE> <PRE class = jav A name = "code">/* 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 (@" --------------- PrintNumBlock ---------------------> ");/* separate multiple parameters in code with commas */void (^ printNumBlock) (int, int); printNumBlock = ^ (int num, int num2) {num = num + num2; NSLog (@ "printNum: % d", num) ;}; printNumBlock (random, 1000 ); NSLog (@ "----------------- recursive use --------------------->");/** you should pay attention to the code when using recursion. initialize the entire code before calling it, otherwise it will run incorrectly! 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 */www.2cto.com _ block int localNum = 500; void (^ changLocalNumBlock) (int) = ^ (int I) {localNum = localNum + I ;}; changLocalNumBlock (30); NSLog (@ "changLocalNumBlock: % d", localNum ); </PRE> <PRE class = java name = "code"> refer to this blog: </PRE> <PRE class = java name = "code"> http://blog.sina.com.cn/s/blog_71715bf8010166ux.html </PRE>

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.