ios - 如何在 Swift 3 中加载自定义包

标签 ios swift swift3

在 Objective-C 中我是这样做的:

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

但我似乎无法在 swift 3 中找到等价物

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

但下一步是什么?或者有没有更好的方法来加载自定义包?

最佳答案

使用 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")
}

另一种方法是使用包标识符加载包 (在包的 Info.plist 中定义):

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

关于ios - 如何在 Swift 3 中加载自定义包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39327241/

相关文章:

ios - 将 AVPlayerLayer 添加到 UIViewController

ios - 如何从 Parse 中删除对象?

ios - 当我重新加载表格 View 时,来自调试器 : Terminated due to memory issue, 的消息

java - 支持应用程序 - 从浏览器(桌面和移动)访问系统信息

ios - 以编程方式关闭标记的信息窗口 google maps iOS

iphone - 声明委托(delegate)协议(protocol)

swift - 在 Swift 中你能捕获 "fatal error unexpectedly found nil while unwrapping an optional value"吗?

ios - 快速无限循环的水平 UIScrollView

ios - 错误初始化 AZSCloudStorageAccount Swift 3

ios - Swift 中不调用协议(protocol)委托(delegate)方法