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:Locally: 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!
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!