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
- • Read and write a Package.swift manifest
- • Tell targets (modules of code) from products (what you ship)
- • Declare external dependencies with version rules
- • Wire a dependency into a target with .product(name:package:)
- • Build, test, and run with swift build / swift test
- • Add packages in Xcode via the repo URL
📊 Manifest Vocabulary
Term
Meaning
Package.swift
The manifest (Swift code)
target
A module that gets built
product
A library/executable to share
dependencies
External packages you rely on
.testTarget
A target holding tests
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.
Pro Tips
- 💡 Keep the tools version current at the top of the manifest to unlock newer features.
- 💡 Pin sensible version rules like from: so updates stay within a major version.
- 💡 Split large code into multiple targets for cleaner module boundaries.
- 💡 Commit Package.resolved so teammates get the exact same dependency versions.
Common Errors (and the fix)
- "No such module" — you added the dependency but didn't list it in the target's dependencies .
- Manifest won't parse — the // swift-tools-version line is missing or malformed.
- Version resolution fails — incompatible version rules across packages; relax or align them.
- Tests don't run — the test target isn't declared with .testTarget or lacks a dependency on the code target.
📋 Quick Reference
Task
Code
Notes
Library product
.library(name:targets:)
to share
Code module
.target(name:)
builds source
Tests
.testTarget(name:dependencies:)
unit tests
Dependency
.package(url:from:)
external pkg
Use it
.product(name:package:)
per target
Build
swift build
compile
Frequently Asked Questions
Mini-Challenge: Write a Manifest
Author a Package.swift for a MathKit library with a test target, then build it.
🎉 Lesson Complete!
- ✅ Package.swift is the manifest — written in Swift
- ✅ Targets build code; products share it as libraries or executables
- ✅ List external packages in dependencies with version rules
- ✅ Give a target access via .product(name:package:)
- ✅ Build with swift build , or add packages in Xcode by URL
- ✅ Next lesson: App Architecture with MVVM
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.
Continue this course
- Previous: Unit Testing with XCTest
- Next: App Architecture with MVVM