I have a project comprising multiple frameworks, a couple of which need to initialize UIImage instances. UIImage's initializer defaults to using Bundle.main if you don't specify a Bundle.
This means if the image asset I am trying access is in the current (non-main) bundle, then I need to specify the bundle or it will return nil from UIImage(named: "MyImage").
I am trying to work around this with a clean implementation like so:
fileprivate class _Foo { }
extension Bundle {
static var current: Bundle { .init(for: Foo.self) }
}
public extension UIImage {
static func inCurrentBundle(named name: String) -> UIImage? {
return UIImage(named: name, in: .current, compatibleWith: nil)
}
}
The Bundle extension above is by default internal to the module it is defined in, so my idea was to define a new extension Bundle with a Bundle.current in each module. However, this obviously doesn't work because the .current symbol in the inCurrentBundle method uses the implementation above it and doesn't get "overridden" by another one even if inCurrentBundle is called from a different module.
Can anyone think of a way to tweak this so the approach works without having to specify a bundle?
Note: while I can certainly think of various workarounds, I am specifically trying to come up with an elegant solution which is simple and minimal. I am particularly interested in whether there's any way to provide some skeleton that can be overridden between modules.