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

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

问题描述

当构建我的代码时,在 Alamofire.request(request as!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天全站免登陆