Introducing the Swift Package Manager
Xcode 11’s improved support for the Swift Package Manager simplifies adding and updating third party frameworks in your projects. In this article you’ll learn how to use the Swift Package Manager to add third party frameworks to your apps.
Accessing the Swift Packages
You can access the Swift packages by selecting your project from the project navigator and clicking the Swift Packages button at the top of the project editor.
Initially the list of Swift packages is empty.
Adding a Swift Package to Your Project
Click the Add button to add a package to your project. Enter the name of the package or the URL of the package repository in the search field. You’ll get more accurate results if you know the repository URL. The following screenshot shows a search for the SwiftCheck testing library:
After choosing the package to add, click the Next button.
Specifying Version Rules
The next step is to specify the rules for what version of the package to use. You can choose a version, a branch from the git repository, or a specific git commit. Most of the time, you’ll either use a version or a branch. Using the master branch would ensure you’re using the latest stable version.
Xcode provides four different version rules.
- Up to next major. If the current version of the package is 1.1, it will use the latest 1.x version. It won’t use version 2 of the package.
- Up to next minor. If the current version of the package is 1.1, it will use the latest 1.1.x version. It won’t use version 1.2 of the package.
- Range, which lets you specify the version ranges of the package to use.
- Exact, which uses a specific version of the package.
Xcode defaults to using up to next major. Click the Next button. Xcode will generate the package for you. Click the Finish button.
Viewing the Dependencies
After adding a Swift package to your project, the project navigator will show the Swift package dependencies.
Adding the SwiftCheck package dependency to the project also adds the packages SwiftCheck depends on: FileCheck, llbuild, SwiftPM, and Rainbow. Xcode added the dependencies without me having to do anything. Now I can import the SwiftCheck module into my unit testing classes and use SwiftCheck to generate test cases for me.
Changing a Rule
Suppose you made a mistake specifying a version rule when you added a Swift package to your project. How do you change the rule?
Access the list of Swift packages in your project from the project editor. Double-click on the package to open a sheet to change the rule.