iOS_ObjeC ---- PromiseKit

使用cocospod 导入PromiseKit第三方框架
项目导入之前需要设置一下swift版本4.2,因为框架是ObjeC和Swift通用的
iOS_ObjeC ---- PromiseKit_第1张图片
设置swift第一步.png
iOS_ObjeC ---- PromiseKit_第2张图片
设置swift第二步.png
iOS_ObjeC ---- PromiseKit_第3张图片
PromiseKit.png

然后在需要导入的地方:


iOS_ObjeC ---- PromiseKit_第4张图片
屏幕快照 2019-05-12 下午8.59.39.png

然后就可以使用了:

iOS_ObjeC ---- PromiseKit_第5张图片
屏幕快照 2019-05-12 下午9.00.57.png
- (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;
}

你可能感兴趣的:(iOS_ObjeC ---- PromiseKit)