How to add a module for modularisation using Swift Package Manager

Sashen Singh
3 min readOct 23, 2022

--

The following article is part 2 of a 4-part series on modularisation in iOS with Swift Package Manager and some approaches we may take. If you missed out on part 1, feel free to check it out. As a reference for examples, we will be using this GitHub Repository.

We shall take the ModularisationStarter project and vertically modularise it as per the approach we discussed in part 1 of the series. The current structure of the starter project looks as follows:

ModularisationStarter Project Structure and the sample app built on a simulator

Adding a package

We shall be taking Feature1 and moving it out into its module called Feature1Module. The module will be created using Swift Package Manager. To add a package, within Xcode select File > New > Package

Adding a new package in Xcode

Add an appropriate name to your package in our example we’ll call it Feature1Module and make sure to add it to a Packages folder or create one if it doesn’t exist. Once you’ve created your Packages folder make sure to modify the Add to: and select your current project. As a group select the Packages group you’ve recently created.

Naming and creating our package

Once created, add your supported platforms with targets to the package.swift file of your module. Here we’ll add iOS supporting iOS15 as an example

package swift for our newly created module

Your project structure should now look like this with the newly added package.

Our project with the newly created package

We are then free to move the code from our Main App to the module as per the diagram in part 1.

Moving our code into the new package

In our main app, we need to then make sure to import Feature1Module wherever we may need it, in our case this would be in the AppDependencyContainer.

To show clearer separation within our package, we can create further modules within the Feature1Module. To do this restructure your package as follows

Next, we’ll modify our package.swift file to represent the different targets and the relationships between them.

As per our package.swift above we have exposed two targets(FeatureModule1UI and FeatureImplementations) which can then be used externally outside our package, as in the case of our main app.

Add modules as dependencies

In our example for horizontal modularisation, where we defined each module as a separate package we may need to add links between packages. For example, referring to the Horizontal modularisation example diagram in part 1 we will need a link for our Presentation Module to our Feature Module to use it. To do this add the relevant dependencies to the package.swift file as below

Once this is done we are free to import our Feature module wherever we may need it.

Thank you for taking the time to read this article. Feel free to follow me on Twitter.

Follow up article

In the next part of the series, we’ll learn how to integrate the package into our workflow.

--

--