简体   繁体   English

如何在Swift 3中加载自定义捆绑包

[英]How do I load a custom bundle in Swift 3

In Objective-C I was doing this: 在Objective-C中,我这样做:

NSString *path = [[NSBundle mainBundle] pathForResource:@"LUIImages" ofType:@"bundle"];
path = [[NSBundle bundleWithPath:path] pathForResource:_imageHash ofType:nil];

But I can't seem to find equivalent in swift 3 但我似乎无法在迅捷3中找到相同的东西

let bundlePath: String = Bundle.main.path(forResource: "LiveUI", ofType: "bundle")!

But what is the next step? 但下一步是什么? Or is there any better way to load a custom bundle? 或者有没有更好的方法来加载自定义捆绑?

Use the Bundle(path:) constructor and avoid forced unwrapping: 使用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")
}

An alternative is to load the bundle using the bundle identifier (defined in the bundle's Info.plist): 另一种方法是使用bundle标识符(在bundle的Info.plist中定义)加载bundle:

let bundle = Bundle(identifier: "com.company.bundle.LiveUI")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM