"NSURL" 不能隐式转换为 "URL";您是要使用 "as" 来显式转换吗?

"NSURL" is not implicitly convertible to "URL"; did you mean to use "as" to explicitly convert?

我有最新的 xcode 测试版,只是想在应用程序中加载网页。

import UIKit

class ViewController: UIViewController {

    @IBOutlet var webView: UIWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let url = NSURL (string: "https://www.google.com");
        let requestObj = NSURLRequest(URL: url!);
        webView.loadRequest(requestObj);
    }

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

这一行:let requestObj = NSURLRequest(URL: url!); 我收到以下错误:

"NSURL" is not implicitly convertible to "URL"; did you mean to use "as" to explicitly convert?

我删除感叹号后出现此错误:

Cannot convert value of type "NSURL?" to expected argument type "URL"

在过去的四个小时里,我一直在 Internet 上尝试此代码的变体。我在想最新的 swift 只是不再允许这个或其他什么,因为没有任何效果。我设置为 10.0 部署目标和 Xcode 8.0 兼容。但是我也为这两种设置尝试了早期版本,并且我在这两种设置的所有版本中都遇到了相同的错误。

是的,我在 Storyboard 上的 webView 已正确连接到 ViewController.swift。

在 swift 3 中你需要使用 URL 而不是 NSURL,所以像这样创建 url 对象。

if let url = URL(string: "https://www.google.com") {
    webView.load(URLRequest(url: url))
}

Swift3.1 更新:

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

注意使用 URL 而不是 NSURLload 而不是 loadRequest