在Objective-C中,我这样做:
NSString *path = [[NSBundle mainBundle] pathForResource:@"LUIImages" ofType:@"bundle"];
path = [[NSBundle bundleWithPath:path] pathForResource:_imageHash ofType:nil];
Run Code Online (Sandbox Code Playgroud)
但我似乎无法在迅捷3中找到相同的东西
let bundlePath: String = Bundle.main.path(forResource: "LiveUI", ofType: "bundle")!
Run Code Online (Sandbox Code Playgroud)
但下一步是什么?或者有没有更好的方法来加载自定义捆绑?
Mar*_*n R 21
使用Bundle(path:)构造函数并避免强制解包:
if let bundlePath = Bundle.main.path(forResource: "LiveUI", ofType: "bundle"),
let bundle = Bundle(path: bundlePath),
let path = bundle.path(forResource: "...", ofType: nil) {
print(path)
} else {
print("not found")
}
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用bundle标识符(在bundle的Info.plist中定义)加载bundle:
let bundle = Bundle(identifier: "com.company.bundle.LiveUI")
Run Code Online (Sandbox Code Playgroud)