首先,block是objective C的object, 可以装在NSArray和NSDictionary里面。
我们可以define一个block like this
void (^simpleBlock)(void);block is just like a variable, and you can provide a value to it later. block就像一个class的变量
simpleBlock = ^{
NSLog(@"This is a block");
};
我们也可以定义像这样,就如同一个局部变量
void (^simpleBlock)(void) = ^{
NSLog(@"This is a block");
}
Block也可以带有返回值和参数
double (^multiplyTwoValues)(double, double) =^(double firstValue, double secondValue) {
return firstValue * secondValue;
};
double result = multiplyTwoValues(2,4);
NSLog(@"The result is %f", result);
没有评论:
发表评论