适用于Swift 2.0的Alamofire和SwiftyJSON

时间:2015-10-01 04:05:13

标签: ios swift swift2 alamofire swifty-json

我今天正在为Swift 2.0更新我的代码,但行

var json = JSON(json)给了我以下错误

  

无法为类型' JSON'调用初始化程序使用类型的参数列表   (结果)

你们有什么想法我应该如何改变我的代码?

@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 个答案:

答案 0 :(得分:4)

现在响应对象随之附带,因此您必须使用响应对象中的value属性

所以它将是JSON(json.value!)

例如:

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