Swift 致命错误:在展开可选值(lldb)时意外发现nil

Swift 致命错误:在展开可选值(lldb)时意外发现nil,swift,Swift,当我按下侧边栏上停止在此处的按钮时,出现错误: webView.scalesPageToFit = true webView.loadRequest(request) 我想寻求帮助来解决这个问题。 下面是我的ViewController.swift的代码 import UIKit class ViewController: UIViewController, UISearchBarDelegate { @IBOutlet weak var searchBar: UISearchBar

当我按下侧边栏上停止在此处的按钮时,出现错误:

webView.scalesPageToFit = true
webView.loadRequest(request)
我想寻求帮助来解决这个问题。 下面是我的
ViewController.swift的代码

import UIKit

class ViewController: UIViewController, UISearchBarDelegate {
    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let url = NSURL(string: "http://www.apple.com")
        let request = NSURLRequest(URL: url!)

        webView.scalesPageToFit = true
        webView.loadRequest(request)
    }

    func searchBarSearchButtonClicked(searchBar: UISearchBar! {
        caricaUrl(searchBar.text)
    }

    func caricaUrl(url: String) {
        let url = NSURL(string: "http://www.google.com/search?q=" + "\(url)")

        let request = NSURLRequest(URL: url!)

        webView.scalesPageToFit = true
        webView.loadRequest(request)
    }

    func didReceiveMemoryWaarning() {
        super.didReceiveMemoryWarning()   
    }


    @IBAction func onBurger() {
        (tabBarController as TabBarController).sidebar.showInViewController(self, animated: true)
    }

}

您的web视图是一个可选值,因此您必须在调用它时强制将其展开,或者说它是一个可选值。你的应用程序试图强制展开它,但它返回为零。这意味着您不能强制展开变量

因此,您可以在声明的两个位置更改这两行:

webView.scalesPageToFit = true
webView.loadRequest(request)
对这些行:

webView?.scalesPageToFit = true
webView?.loadRequest(request)

问号明确表示webView变量是可选的。

因此,您的webView似乎为零。您是否记得将其连接到Interface Builder中的插座?