Bundle executable or shareable code into a standalone Swift package.
- Swift Package Manager
Swift packages are reusable components of Swift, Objective-C, Objective-C++, C, or C++ code. Use them to package executable code, for example a script, as an executable product, or create a package that vends shareable code as a library product. You can create Swift packages with Xcode.
Create a Swift Package
To create a new Swift package, open Xcode and select File > New > Swift Package. Choose a name and select a file location. Select “Create Git repository on my Mac” to put your package under version control. On completion, the Swift package opens in Xcode and looks similar to a standard Xcode project. Xcode generates all necessary files and folders as it creates a Swift package:
READMEfile resides at the root level of the package. It describes the functionality of your package.
Packagefile, or package manifest, describes the configuration for the Swift package. You can double-click it in Finder to open the package in Xcode. The manifest file uses Swift and API from the Swift Package Manager’s
Packagelibrary. It defines the package’s name, products, targets, and dependencies on other packages.
Source files reside in a folder named
Sources. A Swift package can contain several targets, and, as a convention, each target’s code resides in its own subfolder.
Unit test targets reside in a folder named
Tests, and, following the same convention as standard targets, each test target’s code resides in its own subfolder.
Configure Your Swift Package
Swift packages don’t use
.xcworkspace but rely on their folder structure and use the
Package file for additional configuration. The following code listing shows a simple package manifest:
Package file needs to begin with the string
// swift-tools-version:, followed by a version number such as
The Swift tools version declares:
The version of the PackageDescription library
The Swift language compatibility version to process the manifest
The required minimum version of the Swift tools to use the package
Each version of Swift can introduce updates to the
Package library, but the previous API version is available to packages that declare a prior tools version. This behavior allows you take advantage of new releases of Swift, the Swift tools, and the PackageDescription library, without having to update your package manifest and without losing access to existing packages.
To learn more about the
Package API, see
Add Your Code
To add source files to a Swift package, you can use workflows that you already know. For example, you can add a source file to a package by dragging it into the Project navigator, or by using the File > Add Files to [packageName] menu. A Swift package’s targets can contain Swift, Objective-C/C++, or C/C++ code, but an individual target can’t mix Swift with C-family languages. For example, a Swift package can have two targets, one that contains Objective-C, Objective-C++, and C code, and a second one that contains Swift code.
Make Your Swift Package Cross-Platform Compatible
While Swift packages are platform-independent by nature and include Linux as a target platform, your Swift packages can be platform-specific. Use conditional compilation blocks to handle platform-specific code and achieve cross-platform compatibility. The following example shows how to use conditional compilation blocks:
In addition, you may need to define a minimum deployment target. Note how the package manifest below declares minimum deployment targets by passing them in as a value to the
platforms parameter of the
package initializer. However, passing minimum deployment targets to the initializer doesn’t restrict the package to the listed platforms.
Build Your Targets and Run Unit Tests
Xcode creates a scheme for each product in the Swift package’s manifest file. If your package contains multiple products, Xcode creates an additional scheme with the name
Select a scheme for the package’s build-and-run destination, and build it as you’d build an app target. Each source target usually has at least one corresponding test target.