iOS 由masory之Block小记
2018-03-05 本文已影响10人
泛坷
回调block
-(void)changeValue:(void(^)())content;
返回Block
-(void(^)())changeValuet;
函数式编程
TextBlock *test = [TextBlock new];
void(^block)(NSString *str) = test.logString;
block(@"dddd");
//上面两行等价于下面的一行
// test.logString(@"66666");
test.logVoid();
//链式编程思维
test.logObj(@"1").logObj(@"2").logObj(@"3");
NSLog(@"======================");
TextBlock.h
-(void(^)(NSString *string))logString;
-(void(^)(void))logVoid;
-(TextBlock *(^)(NSString *obj))logObj;
TextBlock.m
-(void(^)(NSString *string))logString{
return ^(NSString *string){
NSLog(@"%@",string);
};
}
-(void(^)(void))logVoid{
return ^(){
NSLog(@"........");
};
}
-(TextBlock *(^)(NSString *obj))logObj{
return ^(NSString *obj){
NSLog(@"返回对象的打印 %@",obj);
return self;
};
}