Use of Objective-C syntax block

Source: Internet
Author: User

The code block is essentially similar to other variables. The difference is that the data stored in the code block is a function body. Using code blocks, you can pass in the number of parameters and get the return value just like calling other standard functions.

The Escape Character (^) is the block syntax mark. The returned values and the body of the block (that is, executable code) are defined according to the parameter syntax we are familiar ). The syntax for assigning block variables to a variable:

You can call the block object variable by calling the function:
Int result = myBlock (4); // The result is 28

1. the parameter is the code block of NSString *

        void (^printBlock)(NSString *x);        printBlock = ^(NSString* str)        {            NSLog(@"print:%@", str);        };        printBlock(@"hello world!");

The running result is: print: hello world!

2. The code is used in string array sorting.

        NSArray *stringArray = [NSArray arrayWithObjects:@"abc 1", @"abc 21", @"abc 12",@"abc 13",@"abc 05",nil];        NSComparator sortBlock = ^(id string1, id string2)        {            return [string1 compare:string2];        };        NSArray *sortArray = [stringArray sortedArrayUsingComparator:sortBlock];        NSLog(@"sortArray:%@", sortArray);

Running result: sortArray :(

"Abc 05 ",

"Abc 1 ",

"Abc 12 ",

"Abc 13 ",

"Abc 21"

)

3. recursive call of code blocks

To call a code block recursively, the code block variable must be a global variable or a static variable. In this way, when the program starts, the code block variable is initialized and can be called recursively.

        static void (^ const blocks)(int) = ^(int i)        {            if (i > 0) {                NSLog(@"num:%d", i);                blocks(i - 1);            }        };        blocks(3);

Run the print result:

Num: 3

Num: 2

Num: 1

 4. Use local variables and global variables in the code block

You can use and change global variables in a code block.

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;}

Run the print result:

Worldwide: 1001

Worldwide: 1001

Local variables can be used but cannot be changed.

        int local = 500;        void(^block)(void) = ^(void)        {//            local++;            NSLog(@"local:%d", local);        };        block();        NSLog(@"local:%d", local);

Changing the local variable compilation fails in the code block. How can I change local variables in a code block? Add the keyword __block before the local variable.

        __block int local = 500;        void(^block)(void) = ^(void)        {            local++;            NSLog(@"local:%d", local);        };        block();        NSLog(@"local:%d", local);

Running result: local: 501

Locally: 501

Copyright Disclaimer: This article will be published at http://blog.csdn.net/totogo2010/, and you will be welcomed to enjoy the transfer and sharing. Please respect the work of the author. Keep this note and the author's blog link when reprinting. Thank you!


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.