简体   繁体   English

适用于Swift 2.0的Alamofire和SwiftyJSON

[英]Alamofire and SwiftyJSON for Swift 2.0

I am updating my code for Swift 2.0 for today, however the line 我今天要更新Swift 2.0的代码,但是该行

var json = JSON(json) gives me the following error var json = JSON(json)给我以下错误

Cannot invoke intializer for type 'JSON' with an argument list of type (Result) 无法使用类型(结果)的参数列表调用类型为“ JSON”的初始化器

Do you guys have any idea how should I change my code? 你们知道我应该如何更改代码吗?

@IBAction func changePassword(sender: UIBarButtonItem) {
    Alamofire.request(.POST, AppDelegate.kbaseUrl + "users/me/password", parameters: ["old_password": self.oldPasswordTextField.text!, "new_password": self.newPasswordTextField.text!, "confirm_password": self.confirmPasswordTextField.text!], encoding: .JSON)
        .responseJSON {
            (req, res, json) in
            var json = JSON(json)
            if json["meta"]["status"]["code"] == 200 {
                self.navigationController?.popViewControllerAnimated(true)
            }
            let alert = UIAlertView(title: json["meta"]["msg"]["subj"].stringValue, message: json["meta"]["msg"]["body"].stringValue, delegate: nil, cancelButtonTitle: "Close")
            alert.show()
    }
}

Now the response object came with it so you have to use the value property from the response object 现在响应对象附带了它,因此您必须使用响应对象中的value属性

So it will be JSON(json.value!) 因此它将是JSON(json.value!)

For example : 例如 :

Alamofire.request(.GET, "http://api.androidhive.info/contacts/", parameters: nil, encoding: .JSON, headers: nil).responseJSON { (req, res, json) -> Void in
    print("\(res?.allHeaderFields)")
    print("JSON - \(json.value)")

    let swiftJsonVar = JSON(json.value!)
    print(swiftJsonVar)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM