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 *
[CPP]View plaincopy
- 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.
[CPP]View plaincopy
- 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.
[CPP]View plaincopy
- 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.
[CPP]View plaincopy
- 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.
[CPP]View plaincopy
- 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.
[CPP]View plaincopy
- _ 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!
Use of objective-C syntax Block