美文网首页
swift-Promise

swift-Promise

作者: alex_zn | 来源:发表于2018-07-19 14:35 被阅读0次
    //
    //  MyPromise.swift
    //  WeexApp
    //
    //  Created by zzn on 2016/9/28.
    //  Copyright © 2016年 zzn. All rights reserved.
    //
    
    import UIKit
    import PromiseKit
    
    class MyPromise {
    
        init() {
            setup()
        }
        
        func setup() {
    
            login(userName: "lishi", pwd: "1234").then{self.getFriend(id: $0.id)}.then{ (list) -> Void in
                print(list.enumerated())
            }.catch { (err) in
                print(err)
            }
        }
        
    
        //模拟登陆
        func login(userName:String,pwd:String) -> Promise<Person> {
            
            //requestNet
            let isAcc = true
            let person = Person()
            
            return Promise{ fulfile,reject in
            
                if isAcc {
                    person.name = userName
                    person.pwd = pwd
                    fulfile(person)
                } else {
                    reject(MyError.init(info: "HAHA"))
                }
            }
            
        }
        //模拟获取好友列表
        func getFriend(id:String?) -> Promise<[Person]>  {
            
            
            //requestNet
    
            let perList = [Person(),Person()]
            return Promise{ fulfile,reject in
                
                if id?.isEmpty == true {
                    reject(MyError.init(info: "HAHA"))
                }
                
                fulfile(perList)
            }
        }
        
    
    }
    
    class Person {
        
        var name:String?
        var pwd:String?
        var id = "2332"
        
        init() {
            name = "zhanshan"
            pwd = "11"
        }
        
    }
    
    struct MyError:Error {
        
        var info:String
    }
    
    
    

    相关文章

      网友评论

          本文标题:swift-Promise

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