Swift Package Manager
SwiftPM is the official way to organise code and pull in libraries. You'll read a Package.swift manifest, understand targets and products , declare dependencies , and add packages in Xcode and from the terminal.
Learn Swift Package Manager in our free Swift course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Swift course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
What You'll Learn in This Lesson
1️⃣ The Package.swift Manifest
The manifest is real Swift. It names the package, lists products (what consumers use) and targets (modules of code that build them).
Your turn. Fill in the product and target factory methods.
2️⃣ Dependencies
List external packages in the dependencies array with a version rule, then give a target access by naming the dependency's product .
Now you try. Fill in the array that lists external packages.
3️⃣ Building from the Terminal
SwiftPM ships with the toolchain. Build, test, and run a package with a few commands — no Xcode required.
📋 Quick Reference
Author a Package.swift for a MathKit library with a test target, then build it.
Practice quiz
Which file defines a Swift package?
- Info.plist
- Package.swift
- project.pbxproj
- package.json
Answer: Package.swift. Package.swift is the manifest that describes a Swift package.
The Package.swift manifest is written in...
- JSON
- YAML
- Swift
- XML
Answer: Swift. The manifest is real Swift code using the PackageDescription API.
What does a 'target' represent in a package?
- A build setting
- A module of source code (or tests) that is built
- A remote server
- A device
Answer: A module of source code (or tests) that is built. A target is a basic building block: a module of code that gets compiled.
What does a 'product' expose?
- A library or executable made from targets, usable by others
- A test result
- A storyboard
- A provisioning profile
Answer: A library or executable made from targets, usable by others. Products are libraries or executables that targets build into for consumers.
Where do you list external packages your package relies on?
- In targets only
- In Info.plist
- In a .gitignore
- In the dependencies array
Answer: In the dependencies array. The dependencies array of the manifest lists external packages.
How does a target gain access to a dependency?
- Automatically
- By naming it in that target's dependencies
- Via Info.plist
- It cannot
Answer: By naming it in that target's dependencies. A target must list the dependency in its own dependencies to import it.
Which command builds a package from the terminal?
- swift compile
- swift make
- swift build
- spm build
Answer: swift build. swift build compiles the package using SwiftPM.
How do you usually add a package to an app in Xcode?
- File > Add Package Dependencies and paste the repo URL
- Drag a .zip in
- Edit Info.plist
- Copy source files manually
Answer: File > Add Package Dependencies and paste the repo URL. Xcode adds packages via File > Add Package Dependencies using the repo URL.
Specifying .upToNextMajor(from: "1.2.0") controls...
- The Swift version
- The deployment target
- The product name
- The allowed dependency version range
Answer: The allowed dependency version range. Version rules like upToNextMajor pin which dependency versions are allowed.
Test targets are typically declared with...
- .executableTarget
- .testTarget
- .binaryTarget
- .systemLibrary
Answer: .testTarget. .testTarget declares a target containing unit tests.