使用cocospod 导入PromiseKit第三方框架
项目导入之前需要设置一下swift版本4.2,因为框架是ObjeC和Swift通用的
然后在需要导入的地方:
然后就可以使用了:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// 一步一步调用方法
[self first:@"111"].then(^(NSString *m){
NSString *message = @"222";
return [self second:message];
}).then(^(NSString *mess){
return [self three:mess];
}).catch(^(NSError *error){
NSLog(@"%s error: %@",__PRETTY_FUNCTION__ ,error);
});
}
- (AnyPromise *)first:(NSString *)message
{
AnyPromise *promise = [AnyPromise promiseWithResolverBlock:^(PMKResolver _Nonnull resolve) {
NSLog(@"%s message:%@",__PRETTY_FUNCTION__,message);
if ([message isEqualToString:@"111"]) {
resolve(message);
}else{
NSError *error = [NSError errorWithDomain:@"" code:1 userInfo:@{NSLocalizedDescriptionKey:@"111参数有误"}];
resolve(error);
}
}];
return promise;
}
- (AnyPromise *)second:(NSString *)message{
AnyPromise *promise = [AnyPromise promiseWithResolverBlock:^(PMKResolver _Nonnull resolve) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"%s message:%@",__PRETTY_FUNCTION__,message);
if ([message isEqualToString:@"222"]) {
resolve(nil);
}else{
NSError *error = [NSError errorWithDomain:@"" code:1 userInfo:@{NSLocalizedDescriptionKey:@"222参数有误"}];
resolve(error);
}
});
}];
return promise;
}
- (AnyPromise *)three:(NSString *)message{
AnyPromise *promise = [AnyPromise promiseWithResolverBlock:^(PMKResolver _Nonnull resolve) {
NSLog(@"%s message:%@",__PRETTY_FUNCTION__,message);
if ([message isEqualToString:@"333"]) {
resolve(message);
}else{
NSError *error = [NSError errorWithDomain:@"" code:1 userInfo:@{NSLocalizedDescriptionKey:@"333参数有误"}];
resolve(error);
}
}];
return promise;
}