我正在开发一个iOS框架,其中包含一些数据文件。要将它们加载到一个Dictionary
我做这样的事情:
public func loadPListFromBundle(filename: String, type: String) -> [String : AnyObject]? {
guard
let bundle = Bundle(for: "com.myframework")
let path = bundle.main.path(forResource: filename, ofType: type),
let plistDict = NSDictionary(contentsOfFile: path) as? [String : AnyObject]
else {
print("plist not found")
return nil
}
return plistDict
}
如果我在带有框架的操场上使用它,它将按预期工作。
但是,如果我使用嵌入在应用程序中的框架,那么它将不再起作用,“路径”现在指向应用程序的捆绑包,而不是框架的捆绑包。
如何确保访问框架捆绑包?
编辑:上面的代码位于框架,而不是应用程序中。
EDIT2:上面的代码是一个实用函数,并且不是结构或类的一部分。
用途Bundle(for:Type)
:
let bundle = Bundle(for: type(of: self))
let path = bundle.path(forResource: filename, ofType: type)
更新:
或通过以下方式搜索捆绑包identifier
(框架捆绑包ID):
let bundle = Bundle(identifier: "com.myframework")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句