'Add a xib file to a Swift Package

I want to start using modular code with the new Swift Package Manager integration with Xcode 11.

The problem is that I can't seem to add any kind of UI files to my new package. I just need to add a xib file.

How is this accomplished?



Solution 1:[1]

Currently Swift packages only support source code so you can't add a xib file to the package. Read the What's Missing? section in the following article:

Creating Swift Packages in Xcode

UPDATE

Xcode 12 allows you to add files that are not source code files to Swift packages. I have not created a Swift package so I cannot provide detailed instructions on how to add a xib file to a Swift package. But as @ffritz mentioned in a comment to this answer, Apple's documentation has an article on bundling resources in Swift packages.

Bundling Resources with a Swift Package

Solution 2:[2]

Thanks to Marks answer above and the Apple docs he shared, I was able to solve my problem with loading .xib files in my Swift Package. I will explain my steps here for future onlookers.

  1. Move all of my .xib files in my new Swift package into a folder named Resources (in location /Sources/PackageName/Resources)

  2. Change any code I have which loads these .xib files programatically to include the module bundle. E.g:

     super.init(nibName: "MyViewController", bundle: nil) //Old way of initialising
    
     super.init(nibName: "MyViewController", bundle: Bundle.module) //New way of initialising
    

Solution 3:[3]

When you create a Swift package with resources Bundle gains automatic static identifiers per package e.g a package called Moon would be accessed via Bundle.moon

e.g to instantiate a cell with a XIB called Crater.xib in Moon you would use

let nib = UINib(nibName: "Crater", bundle: Bundle.moon)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Harry Bloom
Solution 3 Warren Burton