在多个位置调用捆绑构造函数在性能方面是否昂贵?



我需要在应用程序的不同部分使用bundleWithPath:至少1000次。

func someMethod() -> String {
let path = Bundle(for: SomeClass.self).path(forResource: "someResource"),
let bundle = Bundle(path: path)
return bundle.someMethodReturningString
}

捆绑(路径在性能方面是否昂贵? 我是否需要保留引用以避免多个实例?

根据文档,答案是否定的

仅当没有与 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.

目标-C

+ 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.

最新更新