【问题标题】:(Swift) Error when trying to load a local .css file(Swift) 尝试加载本地 .css 文件时出错
【发布时间】:2020-02-10 17:10:54
【问题描述】:

在我的应用程序中,我有一个名为 "CSS" 的文件夹,其中包含名为 "style.css" 的文件。
当我尝试加载它时,我收到此错误:

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

这是我试图加载它的代码:

let path = Bundle.main.path(forResource: "style", ofType: "css", inDirectory: "CSS")!
let cssString = try! String(contentsOfFile: path).trimmingCharacters(in: .whitespacesAndNewlines)

【问题讨论】:

标签: ios swift uiwebview wkwebview


【解决方案1】:

确保您的文件存在于 Build Phases > Copy Bundle Resources 列表中

不要强制解包,检查这样的错误

   guard Bundle.main.path(forResource: "style", ofType: "CSS") != nil else {
        return
    }

    do {

    } catch let error {
        print(error)
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 1970-01-01
    • 2019-07-24
    • 2014-07-28
    • 2011-04-02
    • 1970-01-01
    相关资源
    最近更新 更多