我正在尝试获取 plist
文件的 pathForResource
,当文件在 mainBundle 中时很容易,我使用了:
let path = NSBundle.mainBundle().pathForResource("Settings", ofType: "plist")
但现在我将 settings
文件移到了另一个包中,但我不知道如何获取它。我尝试使用 forClass
和 allBundles
但我是一个敏捷的菜鸟,并没有设法让它工作。也无法通过网络找到解决方案。似乎所有 NSBundle
用法示例仅适用于 mainBundle
最佳答案
如果您不知道哪个包有您的资源,您可以遍历所有资源:
let bundles = NSBundle.allBundles()
var path : String?
var myBundle : NSBundle?
for bundle in bundles {
if let resourcePath = (bundle as! NSBundle).pathForResource("Settings", ofType: "plist") {
path = resourcePath
myBundle = bundle
break
}
}
另一方面,如果您确实有您的包的路径
或标识符
,您可以使用:
let bundle = NSBundle(path: "the-bundle-path")
or
let bundle = NSBundle(identifier: "bundle-identifier")
这与您的 Swift 编程知识水平无关,而与 NSBundle
公开的接口(interface)有关。你应该检查 docs看看他们是否可以帮助您。
关于ios - 从非 mainBundle 的 NSBundle 获取文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31290219/