gpt4 book ai didi

swift - "NSURL"不能隐式转换为 "URL";您的意思是使用 "as"进行显式转换吗?

转载 作者:IT王子 更新时间:2023-10-29 05:20:53 27 4
gpt4 key购买 nike

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

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"

在过去的四个小时里,我一直在尝试通过互联网对这段代码进行变体。我在想最新的 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))
}

关于swift - "NSURL"不能隐式转换为 "URL";您的意思是使用 "as"进行显式转换吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38931870/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com