Block is a strange thing. Not learned before, now look at it, still feel very strange.
Helpless, then the bullet-brace learned.
have parameter return value
Format:
return value type (^ variable name) (parameter type and number) = ^ (formal parameter list) {
code block statements;
Return
};
eg
Int (^plus) (int a,int b) = ^ (int a,int b) {
return a+b;
};
NSLog (@ "%d", plus (2,3));
1. The return value type is int;
2. Variable name is plus;
3. The parameter type is int, the number is separated by commas;
4. In the back, the formal parameters are to be written separately;
5. Add the statement to be written in curly braces;
6. End with a semicolon;
7. When called, add () after the Block method.
This is the equivalent of the formula. Before superficial understanding, now beg also not to pass.
For a block with no return value, there is no return value, which can be divided into parameters and no parameters.
Simply to execute the statement inside. That's how I understand it.
The parameter has no return value:
Format:
void (^ variable name) (parameter type and number) = ^ (formal parameter list) {
code block statements;
};
eg
void (^MYBLOCK1) (int,int) = ^ (int a, int b) {
NSLog (@ "a+b=%d", a+b);
};
(MYBLOCK1);
No parameter has a return value
Format:
void (^block variable name) () = ^ () {
The statement of the code block;
};
Optimization:
void (^block variable name) () = ^{
code block statements;
};
void (^MYBLOCK4) () =^{
NSLog (@ "I'm fine");
};
After summing up the above, I feel a lot clearer. The values are also used in the UI. Later in the work to be more summary, more practice.
Light says do not practice false bashi.
Tween
typedef forgot to write.
typedef, in fact, defines the block as a type. This is much more convenient when there are many different variables for the same type.
typedef void (^block) ();
----equivalent to defining a block type. We can arbitrarily define this type of variable.
Block B1 = ^{
NSLog (@ "HelloWorld");
};
B1 ();
Defines the block type with parameter/return value
Define a new Alias
The return value is int, with two arguments of type int
typedef int (^newtype) (int,int);
NewType New1 = ^ (int a,int b) {
return a+b;
};
Define variables for multiple Newtype types consecutively
NewType new2,new3,new4;
New1 = ^ (int x,int y) {
Return x>y?x:y;
};
int s = new1 (2,3);
NSLog (@ "s =%d", s);
Relatively speaking, it is very convenient and fast.
Block IOS block