Use of code blocks (block) for OBJECTIVE-C syntax

Source: Internet
Author: User

Code blocks are essentially similar to other variables. The difference is that the code block stores the data as a function body. With code blocks, you can pass in the number of parameters as you would call other standard functions, and get the return value.

The caret (^) is the syntax tag for the block. The return value defined by our familiar language rules and the body of the block (that is, code that can execute). How to assign a block variable to a variable's syntax explanation:


Calling the Block object variable in the way it calls the function will be able to:
int result = Myblock (4); Result is 28.

1. The number of references is the code block of nsstring*

        void (^printblock) (NSString *x);        Printblock = ^ (nsstring* str)        {            NSLog (@ "print:%@", str);        };        Printblock (@ "Hello world!");
The execution results are:Print:hello world!

2. The code is sorted in a string array

        Nsarray *stringarray = [Nsarray arraywithobjects:@ "ABC 1", @ "abc", @ "abc", @ "abc", @ "abc", nil];        Nscomparator Sortblock = ^ (ID string1, id string2)        {            return [string1 compare:string2];        };        Nsarray *sortarray = [Stringarray sortedarrayusingcomparator:sortblock];        NSLog (@ "sortarray:%@", Sortarray);
Execution Result:Sortarray: (

"ABC 05",

"ABC 1",

"ABC 12",

"ABC 13",

"ABC 21"

)

3. Recursive invocation of code blocks

Code blocks want to be called recursively, the block variable must be a global variable or a static variable, so that the code block variable is initialized when the program starts, and can be called recursively

        static void (^ const blocks) (int) = ^ (int i)        {            if (i > 0) {                NSLog (@ "num:%d", I);                Blocks (i-1);            }        };        Blocks (3);
To perform the printed result:

Num:3

Num:2

Num:1

 4. Use local variables and global variables in code blocks to use and change global variables in code blocks

int global = 1000;int main (int argc, const char * argv[]) {    @autoreleasepool {        void (^block) (void) = ^ (void)        {            global++;            NSLog (@ "global:%d", global);        };        Block ();        NSLog (@ "global:%d", global);    }    return 0;}

To perform the printed result:

global:1001

global:1001

Local variables can be used, but they cannot be changed.

        int local = $;        void (^block) (void) = ^ (void)        {//            local++;            NSLog (@ "local:%d", local);        };        Block ();        NSLog (@ "local:%d", local);
Changing the local variable compilation in the code block does not pass. How do you change local variables in a block of code? Precede a local variable with Keyword:__block

        __block int local = n;        void (^block) (void) = ^ (void)        {            local++;            NSLog (@ "local:%d", local);        };        Block ();        NSLog (@ "local:%d", local);
Execution Result:local:501

local:501

Copyright statement: This article by http://blog.csdn.net/totogo2010/Original, welcome reprint share. Please respect the author Labor, reproduced when the statement and the author of the blog link, thank you !




Use of code blocks (block) for OBJECTIVE-C syntax

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.