什么是Block?
返回类型(^代码块名)(参数列表)
调用:
代码块名=^(参数列表){};
int (^Sum)(int n1,int n2,int n3);
Sum = ^(int n1,int n2,int n3){
int max = n1;
if (n1 < n2)
{
max = n2;
}
if (n2 < n3)
{
max = n3;
}
return max;
};
调用:
int result = Sum(1,2,3);
block声明:
void (^jack)(NSString *string);
block的实现:
jack = ^(NSString *string){
NSLog(@"%@",string);
};
调用:
jack(@"Rick");
block声明:
void(^myBlockOne)();
block的实现:
myBlockOne=^()
{
num++;
NSLog(@"num=%d",num);
}
或
void(^myBlockOne)()=^()
{
num++;
NSLog(@"num=%d",num);
}
调用:
myBlockOne();