SwiftDeveloper SwiftDeveloper - 4 months ago 161
Swift Question

Swift 3 WKWebView 'URLRequest'; did you mean to use 'as' to explicitly convert? ( BUG )

Hello when I use WKWebView codes with Swift 3 gives me this error

'URLRequest'; did you mean to use 'as' to explicitly convert?

I think this is bug I need help or ideas ? My codes under below

import UIKit
import WebKit

class SocialsViewController: UIViewController, WKNavigationDelegate {

var webView = WKWebView()

override func viewDidLoad() {

let url = NSURL(string: "https://facebook.com")!
webView.loadRequest(NSURLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true




Use URL and URLRequest instead:

let url = URL(string: "https://facebook.com")!
webView.load(URLRequest(url: url))

This is quite similar to http://stackoverflow.com/a/37812485/2227743: you either use NSURL and have to downcast it as URL, or you directly use the new Swift 3 structs.

If you follow the error message, your example would become:

let url = NSURL(string: "https://facebook.com")!
webView.load(URLRequest(url: url as URL))

It could be even worse:

let url = NSURL(string: "https://facebook.com")!
webView.load(NSURLRequest(url: url as URL) as URLRequest)

All this works but of course it's much better to start using URL and URLRequest in Swift 3, without using all this downcasting.