【问题标题】:Swift unexpectedly found nil while unwrapping an Optional value WebViewSwift 在展开可选值 WebView 时意外发现 nil
【发布时间】:2016-11-02 15:49:15
【问题描述】:

我正在使用 Swift3 并尝试加载本地 html 文件。

    let htmlFile = Bundle.main.path(forResource: "index", ofType: "html")
    let htmlString = try? String(contentsOfFile: htmlFile!, encoding: String.Encoding.utf8)
    webView.loadHTMLString(htmlString!, baseURL: nil)

遇到错误 致命错误:在展开可选值时意外发现 nil

【问题讨论】:

  • 任何时候你说!你是在要求崩溃。

标签: webview swift3


【解决方案1】:

您需要将 String 方法抛出的异常处理为:

let htmlFile = Bundle.main.path(forResource: "index", ofType: "html")

do {
    let htmlString = try String(contentsOfFile: htmlFile!, encoding: String.Encoding.utf8)
    webView.loadHTMLString(htmlString, baseURL: nil)
} catch {
    // handle exception
}

【讨论】:

    猜你喜欢
    • 2018-09-22
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    相关资源
    最近更新 更多