Reusable icon indicating copy to clipboard operation
Reusable copied to clipboard

Add better support for SwiftPM through the new `Bundled` protocol.

Open jjamminjim opened this issue 4 years ago • 0 comments

Better support for Swift Package Manager through a new Bundled protocol.

✍️ A few of the protocols' (i.e. StoryboardBased, NibLoadable and NibOwnerLoadable) default implementations attempt to load resources from the bundle containing the resource. With this PR it now does this through the Bundled protocol. Each time you declare conformance to StoryboardBased, NibLoadable or NibOwnerLoadable, you will have to provide access to the appropriate bundle. This can be achieved through one of the following methods...

1. For Cocoapod frameworks or main app bundles.

Declare your Reusable based classes to conform to BundledSelf. This provides a default implementation that uses the bundle associated with the class (Bundle(for: self)). In previous versions of the framework this was the default.

final class CustomCell: UITableViewCell, Reusable, BundledSelf { /* And that's it! */ }

2. For Swift Package Manager bundles.

Declare your Reusable based classes to conform to BundledModule. This provides a default implementation that uses the bundle associated with a Swift Package Manager Bundle (Bundle.module).

final class CustomCell: UITableViewCell, Reusable, BundledModule { /* And that's it! */ }

In order for the resource to be loaded from the bundle of your package, you will also need to add the following extension somewhere in your package framework...

public extension BundledModule {
    static var bundle: Bundle {
        Bundle.module
    }
}

3. For other uses.

Provide you own implementation of the static var bundle: Bundle property directly in your Reusable based class.

final class CustomCell: UITableViewCell, Reusable { 
  static var bundle: Bundle {
    return Bundle.myCustomBundle()
  }
}

jjamminjim avatar Apr 06 '21 19:04 jjamminjim