Swift Package Manager
Swift Package Manager is a tool that organizes your project's code and downloads any outside libraries it needs to work.
Initializing a New Package
swift package init creates a new package skeleton, including a Package.swift manifest and a starter source file.
Example: Initializing a New Package
swift package init --type executable
⚠️ Run this command in your terminal.
Building and Running a Package
swift build compiles all of a package's targets, and swift run builds (if needed) and then executes its main executable target.
Example: Building and Running a Package
swift build
swift run
⚠️ Run this command in your terminal.
Running Tests
swift test compiles and runs all test targets declared in the package's manifest, reporting pass/fail results.
Example: Running Tests
swift test
⚠️ Run this command in your terminal.
Common Mistakes
- Trying to run
swift buildorswift runin a folder without aPackage.swiftmanifest file; SPM requires this file to know how to build the project. - Confusing a package's products (what other packages can import) with its targets (the actual source modules being compiled).
- Forgetting
swift testlooks specifically for a target ending inTestsby convention, matching the package's testing setup.
Chapter Summary
swift package initscaffolds a new package, generating aPackage.swiftmanifest.Package.swiftdeclares the package's name, dependencies, targets, and products.swift buildcompiles the package, andswift runbuilds and executes an executable target.swift testruns the package's test targets.
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: