This code used to work in the previousversion of alamofire before swift 2. Now it gives a warning: cast from Result<AnyObject> to Dictionary<String, AnyObject> always fails
.
Alamofire.Manager.sharedInstance.request(.POST, url, parameters:params)
.responseJSON { (request, response, data) -> Void in
var result = data as? Dictionary<String,AnyObject> //this gives an error cast from Result<AnyObject> to Dictionary<String, AnyObject> always fails
How can I get the cast to dictionary working?
You need to call:
Alamofire.request(.POST, url, parameters:params)
.responseJSON { request, response, result in
debugPrint(result)
if let value = result.value as? [String: AnyObject] {
print(value)
}
}
You should read through the updated README code samples.
I know it's bit too late to answer this but I share this because I feel like maybe this code can help someone:
Alamofire.request(url, method: .post, parameters: param, encoding: JSONEncoding.default, headers: nil).responseJSON
{
response in
SVProgressHUD.dismiss()
let data = response.result.value
let responseObject = data as? NSDictionary
switch (response.result)
{
case .success(_):
print(responseObject!["message"] as! NSString as String)
break
case .failure(_):
SVProgressHUD.showError(withStatus: (responseObject!["message"] as! NSString as String))
print(responseObject!["message"] as! NSString as String)
break
}
}
Thanks and Enjoy! Happy coding! :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With