【问题标题】:Alamofire and SwiftyJSON for Swift 2.0用于 Swift 2.0 的 Alamofire 和 SwiftyJSON
【发布时间】:2015-10-01 04:05:12
【问题描述】:

我今天正在更新我的 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()
    }
}

【问题讨论】:

    标签: ios swift swift2 alamofire swifty-json


    【解决方案1】:

    现在响应对象随之而来,因此您必须使用响应对象中的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)
    }
    

    【讨论】:

    • @Happiehappie 欢迎随时欢迎。
    • @iRealMe 欢迎 :) 你也想问同样的问题?
    • @AshishKakkad 是的,还有一个错误。我可以和你谈谈吗?
    • @iRealMe 是的,随时。
    • 很好的答案...节省了我的时间...谢谢
    猜你喜欢
    • 2014-12-27
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2018-11-12
    • 1970-01-01
    • 2015-04-24
    相关资源
    最近更新 更多