iOS block常见用法
2019-05-27 本文已影响0人
我就是阿泽
1.首先看看block的定义
返回值类型 (^blockName)(参数列表) = ^返回值类型 (参数类型 参数名) { // ...
};
blockName(var);
2.block当做对象的属性
@property(nonatomic, copy) 返回值类型(^属性名)(参数列表);
///1️⃣ block当做属性(无返回值,无参)
@property(nonatomic, copy) void(^test1Block)(void);
///2️⃣ block当做属性(无返回值,有参数)
@property(nonatomic, copy) void(^test2Block)(NSString *Str1,NSString *Str2);
3.block当做OC方法的参数
- (void)methodName:(返回值类型(^)(参数类型 *参数名))block名;
// 其中参数名可省略,为了在引用的时候能自动添加,最好写上
@interface ViewController : UIViewController
/// 3️⃣ block当做方法的参数(无返回值无参数)
- (void)method1:(void(^)(void))block1;
/// 4️⃣ block当做方法的参数(无返回值有参数)
- (void)method2:(void(^)(NSString *str1,NSString *str2))block2;
/// 5️⃣ block当做方法的参数(有返回值有参数)
- (void)method3:(NSString *(^)(NSString *str1, NSString *str2))block3;
@end
4.使用typedef定义block
typedef void(^compleBlock)(void);
@interface ViewController : UIViewController
@property(nonatomic, copy) compleBlock test1;
- (void)method4:(compleBlock)test2;
@end
1.常见用法
进行回调
@interface SecondViewController : UIViewController
@property(nonatomic, copy) void(^test1Block)(void);
- (void)method2:(void(^)(NSString *str1,NSString *str2))block2;
@property(nonatomic, copy) void(^block3)(NSString *str1,NSString *str2);
@end
@implementation SecondViewController
- (IBAction)test3Click:(UIButton *)sender {
if (self.test1Block) {
self.test1Block();
}
}
- (void)method2:(void (^)(NSString * _Nonnull, NSString * _Nonnull))block2 {
NSLog(@"block2 = %@",block2);
if (block2) {
self.block3 = ^(NSString *str1, NSString *str2) {
block2(str1,str2);
};
// self.block3 = block2;
}
}
- (IBAction)test4Click:(UIButton *)sender {
if (self.block3) {
self.block3(@"abc", @"123efg");
}
}
@end
//******************************************
#import "SecondViewController.h"
@implementation ViewController
- (IBAction)test1Click:(UIButton *)sender {
SecondViewController *secondVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"secondVC"];
secondVC.test1Block = ^{
NSLog(@"收到了seconVC 的 block1 属性回调");
};
[secondVC method2:^(NSString * _Nonnull str1, NSString * _Nonnull str2) {
NSLog(@"从secondVC的method2方法中返回的 str1 = %@,str2 = %@",str1, str2);
}];
[self.navigationController pushViewController:secondVC animated:YES];
}
@end
2.进行链式编程
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface NumberManager : NSObject
@property(nonatomic, copy) NumberManager*(^add)(CGFloat number);
@property(nonatomic, copy) NumberManager*(^multiply)(CGFloat number);
@property(nonatomic, assign) CGFloat resultNumber;
//- (instancetype)
@end
NS_ASSUME_NONNULL_END
@implementation NumberManager
- (NumberManager * _Nonnull (^)(CGFloat))add {
__weak typeof(self) weakSelf = self;
return ^NumberManager * _Nonnull(CGFloat number) {
weakSelf.resultNumber += number;
return weakSelf;
};
}
- (NumberManager * _Nonnull (^)(CGFloat))multiply {
__weak typeof(self) weakSelf = self;
return ^NumberManager * _Nonnull(CGFloat number) {
weakSelf.resultNumber *= number;
return weakSelf;
};
}
@end
调用
#import "NumberManager.h"
@implementation ViewController
- (IBAction)test2Click:(UIButton *)sender {
NumberManager *testManager = [[NumberManager alloc] init];
testManager.add(123).multiply(4).add(5).multiply(2);
NSLog(@"结果=%f",testManager.resultNumber);
}
@end
2019-05-27 16:31:42.278 LearnBlock[3062:659032] 结果=994.000000
附上demo地址
GitHub demo地址