Cannot convert value of type 'NSURL?' to expected argument type 'URL'

本小妞迷上赌 提交于 2019-12-04 05:07:10

问题


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)
}

回答1:


For these lines:

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

Replace the NSURL with URL and NSURequest with URLRequest.

Your code should be:

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


来源:https://stackoverflow.com/questions/39549928/cannot-convert-value-of-type-nsurl-to-expected-argument-type-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!