Swift Alamofire +承诺捕捉

时间:2016-08-23 05:05:39

标签: swift2 alamofire promisekit

Folks,以下作品除了expected member name following '.'

之外的catch,xcode错误

这是使用PromiseKit进行宣传的正确方法吗?

欢迎所有建议!谢谢!

@IBAction func loginButtonTapped(sender: AnyObject) {
        let email = userEmail.text!
        let password = userPassword.text!

        func onSuccess(success:Bool, message:String, token: String) -> Promise<Void> {
            if success {
                NSUserDefaults.standardUserDefaults().setBool(true, forKey: "isUserLoggedIn")
                NSUserDefaults.standardUserDefaults().synchronize()
                self.dismissViewControllerAnimated(true, completion: nil)
            } else {
                let myAlert = UIAlertController(title: "Alert", message: message, preferredStyle: UIAlertControllerStyle.Alert)
                let okAction = UIAlertAction(title: "Try Again", style: UIAlertActionStyle.Default, handler: nil)
                myAlert.addAction(okAction)
                self.presentViewController(myAlert, animated: true, completion: nil)
            }
            return Promise { resolve, reject in
                return resolve()
            }
        }

        func onFailure(error:NSError) -> Promise<Void> {
            return Promise { resolve, reject in
                return reject(error)
            }
        }

        Login(email, password: password).then(onSuccess).catch(onFailure)
    }


private func Login(email: String, password: String) -> Promise<(success:Bool, message:String, token: String)> {
    let parameters: [String: String] = [
        "username" : email,
        "password" : password
    ];
    let endpoint = "https://api.foo.bar/login"
    return Promise { resolve, reject in
        Alamofire.request(.POST, endpoint, parameters: parameters, encoding: .JSON)
            .validate()
            .responseJSON { (response) in
                guard response.result.error == nil else {
                    logger.debug(response)
                    let result:(success:Bool, message:String, token: String) = (false, "Login Failed", "")
                    return resolve(result)
                }

                if let value = response.result.value {
                    let apiResponseJSONBody = JSON(value)
                    let result:(success:Bool, message:String, token: String) = (true, "", token: apiResponseJSONBody["token"].string!)
                    return resolve(result)
                }
        }
    }
}

0 个答案:

没有答案