ios - 就性能而言,在多个地方调用捆绑构造函数是否昂贵?
问题描述
我需要在应用程序的不同部分使用 bundleWithPath: 至少 1000 次。
func someMethod() -> String {
let path = Bundle(for: SomeClass.self).path(forResource: "someResource"),
let bundle = Bundle(path: path)
return bundle.someMethodReturningString
}
Bundle(path 在性能方面是否昂贵?我是否需要保留参考以避免多个实例?
解决方案
根据文档,答案是否定的
仅当没有与 fullPath 关联的现有捆绑包时,这些构造函数才会初始化并返回一个新实例,否则它会释放 self 并返回现有对象。
init(for: AnyClass)
Returns the NSBundle object with which the specified class is associated.
init?(identifier: String)
Returns the NSBundle instance that has the specified bundle identifier.
init?(url: URL)
Returns an NSBundle object initialized to correspond to the specified file URL.
init?(path: String)
Returns an NSBundle object initialized to correspond to the specified directory.
+ bundleWithURL:
Returns an NSBundle object that corresponds to the specified file URL.
+ bundleWithPath:
Returns an NSBundle object that corresponds to the specified directory.
+ bundleForClass:
Returns the NSBundle object with which the specified class is associated.
+ bundleWithIdentifier:
Returns the NSBundle instance that has the specified bundle identifier.
- initWithURL:
Returns an NSBundle object initialized to correspond to the specified file URL.
推荐阅读
- java - 在 Java 8 中使用 Consumer 将 Map 写入文件
- c# - 如何在 UWP 中使用后台任务处理包卸载?
- quarkus-qute - Qute 动态创建的模板无法理解对象数据
- list - 测试 L 是否是一个有效的 Prolog 列表
- events - 如何将事件绑定到 ContentView 的 IsVisible 属性?
- r - 仅在特定列中使用非 NA 创建新列 [R]
- java - 引起:java.lang.IllegalArgumentException: Invalid column * with api >=29
- vba - 如何使用使用输入框从用户那里获取的输入在 VBA 中替换它
- c++ - 为什么我的类变量没有正确初始化?
- r - 合并列并创建另一个列以指定源