Alamofire and SwiftyJSON for Swift 2.0

北战南征 提交于 2019-12-22 13:58:53

问题


I am updating my code for Swift 2.0 for today, however the line

var json = JSON(json) gives me the following error

Cannot invoke intializer for type 'JSON' with an argument list of type (Result)

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()
    }
}

回答1:


Now the response object came with it so you have to use the value property from the response object

So it will be 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)
}


来源:https://stackoverflow.com/questions/32879264/alamofire-and-swiftyjson-for-swift-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!