“Init(URL :)”已重命名为“init(url :)” - swift 3 wkwebview错误

时间:2016-07-19 23:57:54

标签: swift url wkwebview xcode8

首次使用webkit和swift 3,我不断收到有关Web视图加载请求的错误。为什么Xcode宣布重命名但保持错误?

var webView: WKWebView!
var websites = ["apple.com", "hackingwithswift.com"]

override func loadView() {
    webView = WKWebView()
    webView.navigationDelegate = self
    view = webView
}

override func viewDidLoad() {
    super.viewDidLoad()

    let url = NSURL(string: "https://" + websites[0])!
    webView.load(NSURLRequest(URL: url as URL) as URLRequest)
    webView.allowsBackForwardNavigationGestures = true
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

2 个答案:

答案 0 :(得分:2)

根据 @LeoDabus 的建议,这里是代码

class Controller: UIViewController {
    var webView: WKWebView!
    var websites = ["apple.com", "hackingwithswift.com"]

    override func loadView() {
        webView = WKWebView()
        webView.navigationDelegate = self
        view = webView
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        let url = URL(string: "https://" + websites[0])!
        webView.load(URLRequest(url: url) as URLRequest)
        webView.allowsBackForwardNavigationGestures = true

    }
}

答案 1 :(得分:0)

我有同样的问题,我使用以下代码行解决了

  let url = NSURL (string: "http://www.sourcefreeze.com")

    let requestObj = NSURLRequest(url: url! as URL)

    webview.loadRequest(requestObj as URLRequest)