Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type 'URL' has no member 'fileURL' - Swift 3

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!

like image 598
Pranav Wadhwa Avatar asked Jul 17 '16 15:07

Pranav Wadhwa


2 Answers

The URL struct in Swift 3 has an initializer for that

let url = URL(fileURLWithPath: path)
like image 155
Luca Angeletti Avatar answered Nov 18 '22 23:11

Luca Angeletti


If you do not use path later, you can write something like this:

let url = Bundle.main.urlForResource("Translation", withExtension: "pdf")
like image 4
OOPer Avatar answered Nov 18 '22 22:11

OOPer