Path to bundle of iOS framework(iOS 框架包的路径)
问题描述
我正在开发一个带有一些数据文件的 iOS 框架.要将它们加载到 Dictionary
我会这样做:
I am working on a framework for iOS, which comes with some datafiles. To load them into a Dictionary
I do something like this:
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
}
如果我在带有框架的游乐场中使用它,它会按预期工作.
If I use this in a playground with the framework, it works as intended.
但是如果我使用嵌入在应用程序中的框架,它就不再工作了,路径"现在指向应用程序的捆绑包,而不是框架.
But if I use the framework embedded in an app, it doesn't work anymore, the "path" now points to the bundle of the app, not of the framework.
如何确保框架的捆绑包被访问?
How do I make sure that the bundle of the framework is accessed?
上面的代码驻留在框架中,而不是在应用程序中.
the code above resides in the framework, not in the app.
上面的代码是一个实用函数,不是结构或类的一部分.
the code above is a utility function, and is not part of a struct or class.
推荐答案
使用Bundle(for:Type)
:
let bundle = Bundle(for: type(of: self))
let path = bundle.path(forResource: filename, ofType: type)
或通过 identifier
(框架包 ID)搜索包:
or search the bundle by identifier
(the frameworks bundle ID):
let bundle = Bundle(identifier: "com.myframework")
这篇关于iOS 框架包的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:iOS 框架包的路径


基础教程推荐
- LocationClient 与 LocationManager 2022-01-01
- Android ViewPager:在 ViewPager 中更新屏幕外但缓存的片段 2022-01-01
- 如何使用 YouTube API V3? 2022-01-01
- :hover 状态不会在 iOS 上结束 2022-01-01
- Android文本颜色不会改变颜色 2022-01-01
- 使用 Ryzen 处理器同时运行 WSL2 和 Android Studio 2022-01-01
- 在 iOS 上默认是 char 签名还是 unsigned? 2022-01-01
- 固定小数的Android Money Input 2022-01-01
- 如何使 UINavigationBar 背景透明? 2022-01-01
- “让"到底是怎么回事?关键字在 Swift 中的作用? 2022-01-01