Reputation: 47871
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?
Upvotes: 14
Views: 12897
Reputation: 779
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! :)
Upvotes: 1