Software Development

Quick and Simple Modularising your project with Swift Packages. Up to date with Xcode 13.2.1

20 kwietnia, 2022 0
Podziel się:

The Swift Package Manager is a tool for managing the distribution of Swift code. It’s great to break down a monolithic application into modules. This process can increase the readability of your project as well as the compilation time of the code, as you don’t need to compile the entire project to work on the single Swift Package module.

In this article, you will learn how to create Swift Package in the fastest and simplest way.

Creating Local Swift Package

For purpose of this tutorial, I created a basic project called SwiftPackageTutorial. This will be the main project, where you will create your local Swift Package. In the next step, you will extract it to a separate repository.

First, inside your main project, create a new group called Modules. This will be the location for all your local modules.

  • Note: This step is not mandatory. You can add your Swift Package to any location within your project. However, if you are planning to have more than one Swift Package and keep all locally (without migrating them to separate repositories), this may help to keep your code more organized.

Choosing: New Group
Now, the simplest way to create a Swift Package inside your project is by going to File => New => Package…

Choosing: File - New - Package
This will open a new Xcode window with the prompt to choose the name and location for your Swift Package. For this example, you can name the package MyFirstSwiftPackage and select your Modules directory. You will also need to choose your main project SwiftPackageTutorial (or workspace if you are using one) from the Add to dropdown and Modules group from Group dropdown. Click Create to finish.

Choosing: Modules
To link the newly created Swift Package with your main project, select your project’s main target (SwiftPackageTutorial) and under the General tab scroll to the Frameworks, Libraries, and Embedded Content section.

Choosing: SwiftPackageTutorial
Press plus (“+”) button, choose MyFirstSwiftPackage static library and press Add to finish.

Choosing: MyFirstSwiftPackage
Well done! You finished creating the first Swift Package. Just make sure if everything works fine.

To test if you can use your local Swift Package, open the main struct from the MyFirstSwiftPackage module and replace

public private(set) var text = "Hello, World!"

with

public private(set) var text = "Hello, World from Swift Package!"

Changing: Hello, World with Hello, World from Swift Package
Next (if you are using SwiftUI) go to your main project’s ContentView struct and replace

Text("Hello, world!")

with

Text(MyFirstSwiftPackage().text)

Don’t forget to also add import MyFirstSwiftPackage on top of your file.

Adding import MyFirstSwiftPackage on top of your file
Press run or reload the preview. You should now see the text saying „Hello, World from Swift Package!”.

Screen with the text:Hello, World from Swift Package

Moving Local Swift Package to Remote

Ok, so now you have your local Swift Package included in your project. You added a bunch of files to it that create your module, but let’s say, you are starting a new project where you would like to reuse that module. In just a few steps, you can extract your local Swift Package into a standalone module hosted on a separate repository.

The easiest method to separate your local Swift Package from the project is to select the package in Xcode’s project navigator panel and drag it to the desired location in Finder.

  • Important: While dragging your local Swift Package from Xcode, it’s important to press and hold the option key. This will create a copy of the package as opposed to moving it to a different location.

Creating a copy
You can now double-click the Package.swift file from the MyFirstSwiftPackage to open it in Xcode.

In Xcode, go to Source Control => New Git Repositories

Choosing: New Git Repositories
Keep MyFirstSwiftPackage checked and press Create to finish.

Creating Git repositories for projects
This will create a local repository with the main branch.

Now you can create or link a local repository with remote in any way you like – it can be through Terminal, any third-party software, or in Xcode. To finish this chapter, I will demonstrate the process when using Xcode.

In your left-hand panel, select Show the Source Control Navigation tab (second from the left), and then Repositories sub-tab. Right-click on the package name MyFirstSwiftPackage to show more options and then press the New “MyFirstSwiftPackage” remote…

Choosing: New "MyFirstSwiftPackage" Remote
For this example, I’m using a GitHub account for hosting my remote repositories.

Description: Used for Tutorial with SwiftPackageTutorial

  • Note: If you don’t have GitHub configured in Xcode, you will need to create Personal Access Token from your GitHub Account under Settings => Developer settings => Personal access tokens.

Replacing Local Swift Package with Remote

Great! Now your Swift Package is in a separate repository. It’s time to replace your local version in the project with the remote version.

Go back to the SwiftPackageTutorial project and remove the local version of the MyFirstSwiftPackage from Project Navigation in Xcode. If you don’t have any other modules in the Modules group, you can also remove the folder.

Choosing: Modules

  • Note: Make sure you press Move to Trash not Remove Reference as you want to completely remove it from the repository.

Don’t forget to remove the linked static library. Go to your project’s main target (SwiftPackageTutorial), and under the General tab scroll to the Frameworks, Libraries, and Embedded Content section. Select MyFirstSwiftPackage and press the minus (“-”) button.

Selecting MyFirstSwiftPackage and pressing the minus (“-”) button
Don’t worry about the errors related to the missing module MyFirstSwiftPackage as you will fix it in a minute.

Press File => Add Packages… to open prompt with all available Swift Packages.

Choosing: Add Packages
In the top-right corner, you will find the Search or Enter Package URL input. Copy-paste the URL of your Swift Package. Here you will find the tutorial.

Since you haven’t added any version tags to the package, you can keep the default configurations for the Dependency Rule which is Branch => main, but if you added version tag already, the recommended rule is Up to Next Major Version (and selecting the latest version).

Choosing: Dependency Rule
Press Add Package which will show you the list of available products included in the package. Keep it as is, and press Add Package again to finish.

Choosing: Add Package
The good news is, that this will automatically add the package to Frameworks, Libraries, and Embedded Content so you don’t need to do it yourself.

Summary

Congrats! You now have your Swift Package hosted on the remote repository and you can use it in your main project. To verify it, you can refresh the preview or run the project and Xcode should no longer show any errors related to the missing module MyFirstSwiftPackage.

Resources

Marek Loose
Autor: Marek Loose
iOS developer with some additional experience in mobile cross-platform solutions, native Android, and Web development. If the weather is nice, he likes to spend his free time flying a paramotor, riding a motorbike, or going for a long walk with his dog Bruno. Otherwise, he spends more time coding.

    Imię i nazwisko (wymagane)

    Adres email (wymagane)

    Temat

    Treść wiadomości

    Zostaw komentarz