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 a code block, you can pass in the number of arguments 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, the code that can be executed) are defined according to the familiar limit number syntax ). Syntax explanation on how to assign 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 number of bytes is the code block of nsstring *.

        void (^printBlock)(NSString *x);        printBlock = ^(NSString* str)        {            NSLog(@"print:%@", str);        };        printBlock(@"hello world!");
The execution 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);
Execution 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 or 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);
Print the result:

Num: 3

Num: 2

Num: 1

 4. Use local variables and global variables in the code block to use and change global variables in the 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;}

Print the 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 keyword :__ block before the local variable

        __block int local = 500;        void(^block)(void) = ^(void)        {            local++;            NSLog(@"local:%d", local);        };        block();        NSLog(@"local:%d", local);
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!




Use of objective-C syntax Block

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.