无法将类型'NSMutableURLRequest'(0x11beb7040)的值强制转换为'Alamofire.URLRequestConvertible'(0x11beb9040) [英] Could not cast value of type 'NSMutableURLRequest' (0x11beb7040) to 'Alamofire.URLRequestConvertible' (0x11beb9040)

查看:77
本文介绍了无法将类型'NSMutableURLRequest'(0x11beb7040)的值强制转换为'Alamofire.URLRequestConvertible'(0x11beb9040)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在构建代码时,然后在 Alamofire.request(请求为!URLRequestConvertible).responseJSON()上崩溃。请建议如何解决此崩溃

when build my code then crash on Alamofire.request(request as! URLRequestConvertible).responseJSON(). pls suggest how to fix this crash

let url = NSURL(string: "https://jsonplaceholder.typicode.com/users")
let request = NSMutableURLRequest(url:url! as URL)
request.httpMethod = "GET"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")

Alamofire.request(request as! URLRequestConvertible).responseJSON(){ response in

       switch response.result
       {
          case .success(let data):                
             print(" i got my Data Yup",data)          
          case .failure(let error):
             print(error)                                
        }
    }


推荐答案

在Swift 3中,您要做的就是将NSMutableURLRequest更改为URLRequest即可。无需投放。
另外,设置var使其可以快速可变。 :)谢谢马库斯发表评论。

In Swift 3, all you have to do is to change NSMutableURLRequest to URLRequest and it will work. No need to cast. Also, set var so it can be swift mutable. :) Thanks, Markus for comment.

这篇关于无法将类型'NSMutableURLRequest'(0x11beb7040)的值强制转换为'Alamofire.URLRequestConvertible'(0x11beb9040)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆