iOS下的闭包上篇-Block
Posted 搜狐技术产品
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS下的闭包上篇-Block相关的知识,希望对你有一定的参考价值。
综上所述:可选的传递某些参数从而实现某些回调功能/ (a->__forwarding->a) = 12;
android自定义ViewGroup的那些事儿 2021-10-14 Flutter之美 2021-10-28 前端通用SEO技术优化指南 2021-10-07 你真的了解符号化么? 2021-09-16ios 中的block应用
在这个大冬天里默默敲着键盘,勿喷.今天学习swift过程中,学习到闭包,发现闭包和oc的block中有很多的相同之处,又重新学习了一下并且学习了一些高级点的用法,内容如下:
void(^demoBlock)() = ^ {
NSLog(@"demo Block");
};
int(^sumBlock)(int, int) = ^(int x, int y) {
return x + y;
};
typedef double(^MyBlock)(double, double);
MyBlock area = ^(double x, double y) {
return x * y;
};
MyBlock sum = ^(double a, double b) {
return a + b;
};
NSLog(@"%.2f", area(10.0, 20.0));
NSLog(@"%.2f", sum(10.0, 20.0));
- (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block;
而最终的结果却是,除了定义类型之外,EnumerateBlock并没有其他用处
NSArray *array = @[@"张三", @"李四", @"王五", @"赵六"];
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"第 %d 项内容是 %@", (int)idx, obj);
if ([@"王五" isEqualToString:obj]) {
*stop = YES;
}
}];
或者:
MyBlock sumBlock = ^(double x, double y) {
return x * y;
};
- (void)add:(int)number withNumber:(int)withNumber sumBlock:(MyBlock) block
{
NSLog(@"执行add:(int)number withNumber:(int)withNumber sumBlock:(void (^)(void))block");
NSLog(@"hahaha--4和2相乘等于:%f",block(number,withNumber));
}
[self add:4 withNumber:2 sumBlock:sumBlock];
6.既然Block是一种数据类型,那么可以将Block当做比较特殊的对象,添加到数组
#pragma mark 定义并添加到数组
@property (nonatomic, strong) NSMutableArray *myBlocks;
int(^sum)(int, int) = ^(int x, int y) {
return [self sum:x y:y];
};
[self.myBlocks addObject:sum];
int(^area)(int, int) = ^(int x, int y) {
return [self area:x y:y];
};
[self.myBlocks addObject:area];
#pragma mark 调用保存在数组中的Block
int(^func)(int, int) = self.myBlocks[index];
return func(x, y);
7.解除循环引用
__weak DemoObj *weakSelf = self;
int(^sum)(int, int) = ^(int x, int y) {
return [weakSelf sum:x y:y];
};
8.今天重点,参考AFN的框架,block根据函数判断正确与否调用success或者failure函数参数,并传入参数.如下:
//由于主要还是用来判断success和failure函数,所以看着不舒服的同学可以自行脑补,将success换成add,将failure换成multiplied
- (void)viewDidLoad {
[super viewDidLoad];
self.isbool = true;//当isbool等于true的时候就相加,否则就相乘
[self setCompletionBlockWithSuccess:^(int x,int y) {
NSLog(@"相加等于:%d",x+y);
NSLog(@"success");
} failure:^(int x,int y) {
NSLog(@"相乘等于:%d",x*y);
NSLog(@"failure");
}];
}
- (void)setCompletionBlock:(void (^)(void))block {
block();
NSLog(@"setCompletionBlock number2:%d number3:%d",self.number2,self.number3);
}
- (void)setCompletionBlockWithSuccess:(void (^)(int x,int y))success
failure:(void (^)(int x,int y))failure
{
self.completionBlock = ^{
self.number2 = 10;
self.number3 = 20;
if (self.isbool == true) {//判断传入哪个参数,执行success还是failure
NSLog(@"开始相加");
success(self.number2,self.number3);//参数传入setCompletionBlockWithSuccess
}else{
NSLog(@"开始相乘");
failure(self.number2,self.number3);//参数传入setCompletionBlockWithSuccess
}
};
}
以上是关于iOS下的闭包上篇-Block的主要内容,如果未能解决你的问题,请参考以下文章