Happiehappie Happiehappie - 2 months ago 19
Swift Question

Alamofire and SwiftyJSON for Swift 2.0

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

Answer

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