Key Takeaways
- The Swift Package Manager (SPM) is a feature added to Swift 3 that allows for package or dependency management, a key component for any language that aims for longevity. Even though it’s not part of the official Swift release, developers can still experiment with it by installing trunk development snapshots of V3.
- Existing Swift packages can be added as a dependency to a project by creating a file named Package.swift and specifying the package’s name and an empty array. A dependency can be added by changing dependencies[] to include the url and specified version of the dependency. The Swift build process will then download the declared dependencies upon the first build.
- Creating a Swift package is similar to creating a normal application, usually consisting of source files located in a Sources directory. Despite potential issues with installing and using the SPM, it is simple to use and create packages for. It forms an integral part of Swift’s journey to becoming a true full-stack language in the near future.
Swift’s journey into a fully fledged cross-platform language continues as it’s contributors focus on the version 3 release. Any language that wants a long-term existence needs a way of adding functionality that doesn’t require the core developers to add every request. This is typically in the form of package or dependency management, and the Swift Package Manager (SPM) will be one of the many features added to Swift 3.
But something not being officially released has never stopped inquisitive developers experimenting in the past. In this article I will introduce the SPM, show you how to install it and existing packages, and how to create your own.
Note: You should be able to follow the steps I will present on an OS X or Linux platform.
Living on the Edge with the Swift Package Manager
The SPM is not part of the current official Swift release and you will need to install trunk development snapshots of V3 to use it. This is not too difficult and you can find instructions here.
As installing this snapshot could break your setup for production app development, I recommend you install swiftenv, which lets you switch between installed versions of Swift and is super useful for experimenting with Swift versions. Once you have installed swiftenv and you have a trunk development release active, check you have the package manager by running:
swift build --version
You will hopefully see something like Apple Swift Package Manager 0.1
.
If you want to use XCode instead then it will manage different Swift versions for you. Open XCode -> Preferences and set the Toolchains under the Components section.
Note: Swift is undergoing rapid development and things change all the time, breaking projects. For this tutorial I used the February 8th snapshot for greater compatibility. Now you can see why I mentioned how to switch versions and you will do it a lot until version 3.
Using Existing Packages
Many existing Swift packages are available, but currently no central listings service like NPM exists, so finding them can be hard. One option is The IBM Swift Package Catalog, but it contains a mixture of CocoaPods, Carthage and Swift packages. I expect there will be an ‘official’ list sometime in the future.
To add an existing package as a dependency to a project, create a file named Package.swift and add the following:
import PackageDescription
let package = Package(
name: "SitePointSPM",
dependencies: []
)
This is the basic structure of a package file where you set a name
, which is a package itself and an empty array. To add a dependency, change dependencies[]
to:
...
dependencies: [
.Package(url: "https://github.com/kylef/Curassow.git", majorVersion: 0, minor: 4),
]
...
This downloads a dependency from a url (generally github) with a specified version using semantic versioning.
Create a Sources folder and in it, create Main.swift. Add the following code:
import Curassow
import Inquiline
serve { request in
return Response(.Ok, contentType: "text/plain", body: "Hello World")
}
This code uses the Curassow and Inquiline packages to configure and start a basic http server.
Execute swift build --configuration release
to run this simple app. Notice that when you build for the first time the Swift build process will download the dependencies declared in your package file, plus the dependencies that it declared.
Creating Your Own Package
You construct a Swift package in the same way as a ‘normal’ application. But a package generally consists of and includes source files located in a Sources directory. The sample application provided by Apple is a great example to learn the potential.
In this example, the PlayingCard package defines a PlayingCard
. Then the DeckofPlayingCards package imports the PlayingCard package and uses it’s methods and objects to create a randomly shuffled Deck
of PlayingCard
s.
Here Be Helpful Dragons
Following this introduction you likely hit problems installing and using the Swift package manager. This shows it’s certainly not ready for production applications. But, it’s simple to use and create packages for and whether you decide to wait for a stable Swift 3, or jump right in and update your code every time something breaks, the Swift package manager is another puzzle piece in making Swift a true full stack language in the next 12 months.
What are your thoughts?
Frequently Asked Questions about Swift Package Manager
What is the Swift Package Manager and how does it work?
The Swift Package Manager is a tool for managing the distribution of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies. It simplifies the process of managing your project’s dependencies by automatically downloading and compiling them for you. It also ensures that the correct versions of your dependencies are used when you build your project.
How does Swift Package Manager compare to other package managers like npm or yarn?
While npm and yarn are used for managing JavaScript dependencies, Swift Package Manager is used for managing Swift dependencies. All three tools serve the same purpose of managing project dependencies, but they are used in different programming environments. Swift Package Manager is integrated with the Swift build system, which makes it more convenient for Swift developers.
How do I install Swift Package Manager?
Swift Package Manager is included in Swift 3.0 and above. Therefore, you don’t need to install it separately. If you have Swift installed on your system, you already have Swift Package Manager.
How do I use Swift Package Manager to manage dependencies?
To manage dependencies with Swift Package Manager, you need to specify them in a Package.swift manifest file in your project. Once you’ve specified your dependencies, you can use the swift build command to download and compile them.
Can I use Swift Package Manager for iOS development?
Yes, you can use Swift Package Manager for iOS development. However, it’s worth noting that it’s not fully integrated with Xcode, the primary IDE for iOS development. Therefore, while you can use it to manage your dependencies, you’ll still need to manually set up your Xcode project to use them.
What are the best practices for managing dependencies with Swift Package Manager?
Some best practices for managing dependencies with Swift Package Manager include specifying exact versions of your dependencies to ensure compatibility, regularly updating your dependencies to get the latest features and security updates, and avoiding unnecessary dependencies to keep your project lightweight.
How do I update dependencies with Swift Package Manager?
To update dependencies with Swift Package Manager, you can use the swift package update command. This will update all of your dependencies to their latest versions that are compatible with your specified version constraints.
Can I use Swift Package Manager with other programming languages?
Swift Package Manager is primarily designed for managing Swift dependencies. However, it also supports C-family languages out-of-the-box, including C, C++, and Objective-C.
How do I troubleshoot problems with Swift Package Manager?
If you’re having problems with Swift Package Manager, you can use the swift package diagnose command to diagnose common issues. If you’re still having trouble, you can consult the official Swift Package Manager documentation or ask for help on the Swift forums.
What are the limitations of Swift Package Manager?
While Swift Package Manager is a powerful tool, it does have some limitations. For example, it doesn’t support binary dependencies, and it’s not fully integrated with Xcode. Additionally, while it does support C-family languages, support for other languages is limited.
Developer Relations, Technical Writing and Editing, (Board) Game Design, Education, Explanation and always more to come. English/Australian living in Berlin, Herzlich Willkommen!