无法转换“NSURL"类型的值?到预期的参数类型“URL" [英] Cannot convert value of type 'NSURL?' to expected argument type 'URL'

查看:34
本文介绍了无法转换“NSURL"类型的值?到预期的参数类型“URL"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这段代码中得到的错误消息是无法转换‘NSURL’类型的值?"到预期的参数类型URL"".当我进入我的网络浏览器时,我可以看到 pdf 文件,所以我知道它正在工作.pdf 文件存储在远程服务器上.请帮忙!

The error message I get from this code segment is "cannot convert value of type 'NSURL?' to expected argument type 'URL'". When I go onto my web browser, I can see the pdf file so I know it is working. The pdf files are stored a remote server. Please help!

override func viewDidLoad() {
    print("dir2: \(dir2) dir1: \(dir1)")
    let targetURL = NSURL(string: "http://example.com/\(dir1)/\(dir2).pdf")
    let request = NSURLRequest(URL: targetURL) //this is the line with error.
    webView.loadRequest(request)
}

推荐答案

对于这些行:

    let targetURL = NSURL(string: "http://example.com/\(dir1)/\(dir2).pdf")
    let request = NSURLRequest(URL: targetURL) 

将 NSURL 替换为 URL,将 NSURequest 替换为 URLRequest.

Replace the NSURL with URL and NSURequest with URLRequest.

您的代码应该是:

    let targetURL = URL(string: "http://example.com/\(dir1)/\(dir2).pdf")
    let request = URLRequest(url: targetURL!) 

这篇关于无法转换“NSURL"类型的值?到预期的参数类型“URL"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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