block OC 用法 函数指针
ex1:
typedef int(^func_aaa)(id parameter);
- (void)testFunc{
func_aaa test;
test = ^(NSNumber* qq){
PKLog(@"val: %@",qq);
return 456;
};
int a = test([NSNumber numberWithInt:123]);
PKLog(@"%d",a);
}
ex1_log:
2016-04-07 17:00:19.028 djcars[5566:210891] 123
2016-04-07 17:00:25.356 djcars[5566:210891] val: 123
2016-04-07 17:00:28.326 djcars[5566:210891] 456
ex2 (在循环内使用Block,block会引用循环体内的值)
typedef int(^func_aaa)(id parameter);
- (void)testFunc{
NSMutableDictionary *mulAry = [[NSMutableDictionary alloc]init];
for (int i = 0 ; i < 10; i++) {
func_aaa test;
test = ^(NSNumber* qq){
PKLog(@"val: %@",qq);
return i+10;
};
[mulAry setObject:test forKey:[NSString stringWithFormat:@"keyNo_is%d",i]];
}
PKLog(@"%@",mulAry);
NSInteger i = 0;
for (func_aaa obj in [mulAry allValues]) {
int v = obj([NSNumber numberWithInteger:i++]);
PKLog(@"%d",v);
}
}
ex2_log:
2016-04-08 10:36:46.129 djcars[2958:46438] val: 0
2016-04-08 10:36:46.129 djcars[2958:46438] 19
2016-04-08 10:36:46.129 djcars[2958:46438] val: 1
2016-04-08 10:36:46.129 djcars[2958:46438] 17
2016-04-08 10:36:46.129 djcars[2958:46438] val: 2
2016-04-08 10:36:46.129 djcars[2958:46438] 15
2016-04-08 10:36:46.130 djcars[2958:46438] val: 3
2016-04-08 10:36:46.130 djcars[2958:46438] 13
2016-04-08 10:36:46.130 djcars[2958:46438] val: 4
2016-04-08 10:36:46.130 djcars[2958:46438] 11
2016-04-08 10:36:46.130 djcars[2958:46438] val: 5
2016-04-08 10:36:46.130 djcars[2958:46438] 18
2016-04-08 10:36:46.130 djcars[2958:46438] val: 6
2016-04-08 10:36:46.130 djcars[2958:46438] 16
2016-04-08 10:36:46.130 djcars[2958:46438] val: 7
2016-04-08 10:36:46.131 djcars[2958:46438] 14
2016-04-08 10:36:46.131 djcars[2958:46438] val: 8
2016-04-08 10:36:46.131 djcars[2958:46438] 12
2016-04-08 10:36:46.131 djcars[2958:46438] val: 9
2016-04-08 10:36:46.131 djcars[2958:46438] 10
ex3 (Block 订阅为类型):
typedef void(^WACallback)(id parameter);
@property(nonatomic, strong)WACallback callbackBlock;
WACallback callbackBlock = ^(id returnValue) {
returnValue = returnValue ? : NSNull.null;
NSDictionary *result = @{@"result": returnValue};
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:result options:0 error:nil];
NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *javascript = [NSString stringWithFormat:@"WebApiCore.invokeWebMethod(\"%@\", %@.result);", callback, json];
[webView stringByEvaluatingJavaScriptFromString:javascript];
};
ex4 (函数内,带参数写法)
void (^updateStringBlock)(nsstring* str1,nsstring* str2) = ^(nsstring* str1,nsstring* str2){
};
ex5 (替代代理写法,要注意强弱引用导致控制器不释放)
@property (nonatomic, copy) void (^onMoreClickBlock)(id obj);
block OC 函数回调语法
//参考语法
+ (void)updataAiQiYiVideo:(NSString*)xxx succeed:(void (^)(NSString* xxx1,NSString* xxx2))succeed fail:(void (^)())fail;
// 上传视频
+ (void)updataAiQiYiVideo:(NSString*)dstPath succeed:(void (^)(NSString* fileId,NSString* dstPath))succeed fail:(void (^)())fail{
NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:@"gameChatTest",@"file_name",@"just_test",@"description",nil];
VCOPClient *client = [PKAiQiYiTool VCOPClientInstance];
[PKAiQiYiTool enterPrise];
[client uploadVideoWithContentOfFile:dstPath fileType:@"mp4" params:params threadCount:3 willStart:^(NSString* filePath, NSString *fileId) {
NSLog(@"fileId=%@",fileId);
} progress:^(NSString *fileId, NSNumber *percent) {
} complete:^(NSString* fileId, NSDictionary *videoInfo) {
//block 成功回调
succeed(fileId,dstPath);
PKLog(@"complete fileId=%@",fileId);
} failure:^(NSString* fileId, NSError *error) {
//block 失败回调
fail();
}];
}