1、引出
1.1、int pp = 3;for循环为dd赋值并 调用 finprint(int ,int)函数 获取pp*dd的乘积,输出
void finalValue(int d, int scale){ printf(".......%d",scale*d);
printf("\n"); } void finprint(int j,int i){ printf("其他操作\n"); void (*fin)(int d,int s) = &finalValue; fin(j,i);
} - (void)viewDidLoad {
[super viewDidLoad];
int dd ;
int pp = 3;
for (int j= 0; j<5; j++) {
dd=j;
finprint(pp,dd);
}
}
a:必须获取finalValue函数的名字,
b: finalValue如果想使用 dd,pp (也即finprint中的形式参数 j和i) 必须传递到finalValue中才可以使用;
1.2、不用必须获取finalValue函数的名字,
void finprint(int j,int i){ printf("其他操作\n"); void (^fina)(int d,int s) = ^(int d,int s){
printf(".......%d",s*d);
printf("\n");
};
fina(j,i);
} - (void)viewDidLoad {
[super viewDidLoad];
int dd ;
int pp = 3;
for (int j= 0; j<5; j++) {
dd=j;
finprint(pp,dd);
}
}
1.3、不用传递参数参数也可以访问finprint中的形式参数 j和i;
void finprint(int j,int i){ printf("其他操作\n"); void (^fina)(void) = ^(void){
printf(".......%d",j*i);
printf("\n");
};
fina();
}
2、 block
2.1、简单概述:带有自动变量(局部变量)的匿名函数;
1)没有函数名
2) 带有”^“号
2.2、语法格式:具体格式
^ 返回值类型 参数列表 表达式
其中 返回值类型 参数列表 可省略
block
How Do I Declare A Block in Objective-C?
As a local variable:
returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...}; As a property:
@property (nonatomic, copy, nullability) returnType (^blockName)(parameterTypes);
As a method parameter:
- (void)someMethodThatTakesABlock:(returnType (^nullability)(parameterTypes))blockName;
As an argument to a method call:
[someObject someMethodThatTakesABlock:^returnType (parameters) {...}];
As a typedef:
typedef returnType (^TypeName)(parameterTypes);
TypeName blockName = ^returnType(parameters) {...};
This site is not intended to be an exhaustive list of all possible uses of blocks.If you find yourself needing syntax not listed here, it is likely that a typedef would make your code more readable.Unable to access this site due to the profanity in the URL? http://goshdarnblocksyntax.com is a more work-friendly mirror.