Function: Save a piece of code that can be executed at any time.
Flag: ^
Similar to a function:
1 can save a section of code
2 has a return value
3 Tangible parameters
4 Invocation Mode
Variable declaration: More function pointer variable similar
return value type (^ variable name) (parameter type list)
typedef
A block with a consistent return value and parameters can be a typedef of the same type
Permissions:
External variables can be accessed inside the block;
However, by default, the outer local variables cannot be modified inside the block;
This variable can be modified inside the block after the local variable is added to the __block keyword.
Example:
Main.m#import <foundation/foundation.h>typedef Int (^mulblock) (int, int)); int main () {//defines a block, and assign the value to the block variable block1void (^block1) = ^{//If there is no formal parameter, you can omit the parameter list NSLog (@ "This is a block");};/ /execute Blockblock1 (), int (^addblock) (int, int)), Addblock = ^ (int a, int b) {return a + B;}; Int (^subblock) (int, int)) = ^ (int a, int b) {return a-A;}; NSLog (@ "%d +%d", Addblock (2, 1), Subblock (2-1)); Mulblock add = ^ (int A, in B) {return a + B;}; Mulblock mul = ^ (int A, in B) {return a * b;}; return 0;}
iOS Review note 10: Using block