我开发了一个 iOS 框架,用户可以将其与 CocoaPod 一起使用。但是在项目中使用的时候,出现了这一行错误:
let url = Bundle(for: type(of: self)).url(forResource: "file_name", withExtension: "html", subdirectory: "assets")!
该行在框架中,错误是:
Unexpectedly found nil while unwrapping an Optional value
在框架中,我有一个名为“assets”的文件夹和一个名为“file_name.html”的文件。仅当在框架中使用时才会出现该错误。
最佳答案
let url = Bundle(for: type(of: self)).url(forResource: "file_name", withExtension: "html", subdirectory: "assets")!
-------------------------------^
确保 self
指向框架的一个类,以便找到正确的包。并且您不需要使用 type(of:_)
:
let bundle = Bundle(for: FrameworkClass.self)
let url = bundle.url(forResource: "file_name", withExtension: "html", subdirectory: "assets")!
关于ios - 开发iOS框架时如何在WKWebView中加载本地HTML文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55517523/