美文网首页
iOS_ObjeC ---- PromiseKit

iOS_ObjeC ---- PromiseKit

作者: 叶舞清风 | 来源:发表于2019-05-12 21:01 被阅读0次
    使用cocospod 导入PromiseKit第三方框架
    项目导入之前需要设置一下swift版本4.2,因为框架是ObjeC和Swift通用的
    
    设置swift第一步.png 设置swift第二步.png PromiseKit.png

    然后在需要导入的地方:


    屏幕快照 2019-05-12 下午8.59.39.png

    然后就可以使用了:

    屏幕快照 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

          本文链接:https://www.haomeiwen.com/subject/hdshaqtx.html