问题
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