Blocks Block Code Snippet
Block encapsulates a piece of code that can be executed at any time. It is an extension of the function in C, which is actually implemented in C, so it is valid in all languages based on C, including Objective-c, C + +, and objective-c++.
A block can be used as a function parameter or as a return value for a function, and it can have either an input parameter or a return value. It is similar to a traditional function pointer, but it is different:
A block is an inline function, and by default it is read-only to local variables.
Apple's official proposal to use block as much as possible. In multi-threaded, asynchronous tasks, collection traversal, collection sorting, animation transitions with a lot.
1 defining code blocks
<returntype> (^blockname) (list of arguments) = ^ (arguments) {body;};
The compiler can infer the return type from the contents of the code block, so it can be omitted. If there are no parameters in the code block, you can omit them.
Int (^sum) (int, int) = ^ (int a, int b) {
return a+b;
}
The = sign is preceded by the definition of the code block, and the equals sign is followed by the implementation content.
Defines a block object called Sum, with two int arguments, and returns an int. The right side of the = type is the concrete implementation of block.
2 Using code blocksThe code block is declared as a variable, so it can be used like a function sum (2,22); This line of code does not have a power symbol, because it is required only when the code block is defined and implemented, and is not required when called.
Int (^sum) (int, int) = ^ (int a, int b) { NSLog (@ "Block Sum is called"); return a+b;}; NSLog (@) before the sum is called. "); int a = Sum (2,22); NSLog (@ "a=%i", a);
Output Result:
2016-02-25 19:14:46.709 command Line project [924:132,247] before calling sum. 2016-02-25 19:14:46.710 command Line project [924:132247] Block Sum was called 2016-02-25 19:14:46.710 command line project [924:132247] a=24
This will fix the block and only use the sum call, and we can usually use a typedef.
3 using typedef keywords
typedef int (^mysum) (int, int);
A block variable is declared with the type MySum. With TypeDef, you can declare an infinite number of block variables for ease of use.
#import <foundation/foundation.h>typedef Int (^mysum) (int, int), int main (int argc, const char * argv[]) { @auto Releasepool { MySum sum1 = ^ (int a, int b) { return a + b; }; MySum sum2 = ^ (int a, int b) { return a*b; }; NSLog (@ "Call sum1 =%i", Sum1 (10,10)); NSLog (@ "Call sum2 =%i", sum2 (10,10)); } return 0;}
Output:
2016-02-25 19:21:22.574 command Line project [1002:135,364] call sum1 =202016-02-25 19:21:22.575 command Line project [1002:135,364] Call sum2 =100program Ended with exit code:0
4 using code blocks directly
Using block usually does not require creating a code block variable, but rather inline the contents of a block of code in code.
Nsarray *stringarray = [Nsarray arraywithobjects:@ "any", @ "Hello", @ "body", @ "good", @ "cool", nil]; Ascending order Nsarray *sortedarray = [Stringarray sortedarrayusingcomparator:^ (ID string1, id string2) { //nslog (@ " string1=%@,string2=%@ ", string1,string2); return [string1 compare:string2]; }]; NSLog (@ "sortarray:%@", Sortedarray);
Output Result:
2016-08-15 14:34:06.257 command Line project [1478:103112] Sortarray: (any , body, cool, good, hello)
5 Local Variables
Blocks can access local variables, but cannot be modified. (By default)
#import <foundation/foundation.h>typedef Int (^mysum) (int, int), int main (int argc, const char * argv[]) { @auto releasepool { int local = +; __block int va =; MySum sum = ^ (int a, int b) { NSLog (@ "local =%i,va =%i", local, VA); local++; Variable is not assignable (misssing __block typedef specifier) va++; Return a + b +va; }; NSLog (@) before the sum is called. "); int a = SUM (10,10); NSLog (@) After the sum is called. "); NSLog (@ "va =%i", VA); NSLog (@ "a=%i", a); } return 0;}
Output:
2016-02-25 19:23:34.909 command Line project [1028:136,661] before calling sum. 2016-02-25 19:23:34.910 command-line project [1028:136661] local = 100,va = 1102016-02-25 19:23:34.910 command-line engineering [1028:136,661] after calling sum. 2016-02-25 19:23:34.910 command Line project [1028:136661] va = 1112016-02-25 19:23:34.910 command line project [1028:136661] A=131program ended with Exi T code:0
As can be seen from the above example, local variables can not be changed in the block, if the changes will be warned?? :
Variable is not assignable (misssing __block typedef specifier)
The local variable VA can be modified, plus __block (two underscores).
Some variables cannot be declared as __block types:
There is no variable-length array;
There is no struct with a mutable array.
Local variable Value
Double A1 = 10,b =; Sampleblock sample = ^{ return a1*b; }; NSLog (@ "%f", sample ()); a1 =; b =; NSLog (@ "%f", sample ());
Results:
2016-08-15 14:43:44.172 command Line project [1573:107,965] 200.0000002016-08-15 14:43:44.175 command line project [1573:107,965] 200.000000
I just started thinking the second one would output 1000, but it wasn't.
This is because the variables are local variables, and the code blocks copy and save their state as they are defined, so the output is the same two times.
6 Global Variables
What would be the result of changing the above variable to a global variable?
static double A1 = 10,b =; Sampleblock sample = ^{ return a1*b; }; NSLog (@ "%f", sample ()); a1 =; b =; NSLog (@ "%f", sample ());
Results:
2016-08-15 15:00:22.142 command Line project [1633:113,895] 200.0000002016-08-15 15:00:22.142 command line project [1633:113,895] 1000.000000
This indicates that the value of the global variable is not copied when the block is defined. and block can change the value of the global variable.
Objective-c 14 Code blocks block