Block is used to save a piece of code
Block's logo: ^
Block is like a function:
1. Can save the Code
2. There is a return value
3. Tangible parameters
4. Call the same way
Defining a block variable
void (^myblock) () = ^{
NSLog (@ "----------------");
NSLog (@ "----------------");
};
Use the block variable to invoke the code inside the block;
Myblock ();
Int (^sumblock) (int,int) = ^ (int a, int b) {
return a + B;
};
int c = Sumblock (10,11);
/*
pointer function pointer
Int (*p) (int,int) = sum;
int d = p (10,12);
NSLog (@ "%d", d);
*/
/*
What the block needs to know.
1> How to define a block variable
Int (^sumblock) (int, int);
void (^myblock) ();
2> How to use block encapsulation code
^ (int a, int b) {
return a-B;
};
^() {
NSLog (@ "----------");
};
^ {
NSLog (@ "----------");
};
3> block access outside variables
* External variables can be accessed inside the block
* By default, the outside local variables cannot be modified inside the block
* Add __block keyword to local variable, this local variable can be modified inside block
4> defining block types with typedef
typedef int (^myblock) (int, int);
You can then use this type of Myblock to define the block variable
Myblock Block;
Myblock B1, B2;
B1 = ^ (int a, int b) {
return a-B;
};
Myblock B3 = ^ (int a, int b) {
return a-B;
};
*/
Oc--block