任何人都可以帮助我理解为什么未在此处初始化我的webView的原因。我收到以下错误,因为webView为零。
Fatal error: Unexpectedly found nil while unwrapping an Optional value
我在这里到底想念什么?
import UIKit
import WebKit
class MemoriesViewController: UIViewController, WKNavigationDelegate {
@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear( animated )
let urlString:String = "https://www.apple.com"
let url:URL = URL(string: urlString)!
let urlRequest:URLRequest = URLRequest(url: url)
webView.load(urlRequest)
}
}
}
该屏幕截图可更好地说明错误的实际发生位置:
答案 0 :(得分:4)
将Webkit.Frameworks添加到链接的框架和库中
然后您可以添加WKWebView的Outlet并使用您的代码
import UIKit
import WebKit
class MyWebViewController: UIViewController {
@IBOutlet weak var myWV: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let urlString:String = "https://www.apple.com"
let url:URL = URL(string: urlString)!
let urlRequest:URLRequest = URLRequest(url: url)
myWV.load(urlRequest)
}
}
答案 1 :(得分:0)
问题是我的输出未连接到情节提要。这就是@IBOutlet was not getting initialized
的原因。
答案 2 :(得分:0)