In Swift 2, I had used the following code:
let path = NSBundle.mainBundle().pathForResource("Document", ofType: "pdf")!
let url = NSURL.fileURLWithPath(path)
webView.loadRequest(NSURLRequest(URL: url))
Now, using Xcode 8 and Swift 3, Xcode automatically translated it to:
let path = Bundle.main.pathForResource("Translation", ofType: "pdf")!
let url = URL.fileURL(withPath: path)
webView.loadRequest(URLRequest(url: url))
On the second line, with the declaration of url
, Xcode gives me the following error:
Type 'URL' has no member 'fileURL'
How can I fix this error? Thanks!
The URL
struct in Swift 3 has an initializer for that
let url = URL(fileURLWithPath: path)
If you do not use path
later, you can write something like this:
let url = Bundle.main.urlForResource("Translation", withExtension: "pdf")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With