Using Swift Packages: Adding Dependencies to Your Xcode Project
When the toys in Andy’s room need something they don’t have — like a manual for Buzz Lightyear’s spaceship or a map of the neighborhood — they borrow it from someone who does. Swift packages work the same way: instead of building everything from scratch, you can add pre-built libraries created by other developers directly into your Xcode project.
You’ll learn what Swift packages are, how to add them to your project through Xcode’s interface, how version rules work, and a few popular packages worth knowing about. We won’t cover creating your own packages — that’s a separate post for when you’re ready to share your code with the world.
Apple Docs: Adding Package Dependencies to Your App — Xcode
This guide assumes you’re familiar with Xcode essentials.
What You’ll Learn
- What Is a Swift Package?
- Adding a Package in Xcode
- Understanding Version Rules
- Importing and Using a Package
- Popular Packages Worth Knowing
- Common Mistakes
- What’s Next?
What Is a Swift Package?
A Swift package is a bundle of reusable code that someone has published for other developers to use. Packages are distributed through the Swift Package Manager (SPM), which is built directly into Xcode — no extra tools required.
Think of packages like the toy accessories in Andy’s room. Rex didn’t come with a roar amplifier, but if one existed in a catalog, Andy could order it and snap it right on. Similarly, your app doesn’t come with networking helpers or image caching, but you can add packages that provide these features with just a few clicks.
Each package lives in a Git repository (usually on GitHub) and contains a file called Package.swift that describes
what the package provides and what it depends on.
Adding a Package in Xcode
Adding a package to your project is done entirely through Xcode’s graphical interface. Here’s the step-by-step process:
- Open your project in Xcode.
- Go to File → Add Package Dependencies… (or click your project in the navigator, select the project name, then the Package Dependencies tab).
- In the search bar, paste the package’s repository URL. For example, to add the popular Kingfisher image-loading
library, you’d paste:
https://github.com/onevcat/Kingfisher.git - Xcode fetches the repository and shows you the available versions. Choose a version rule (more on this in the next section).
- Click Add Package.
- Xcode asks which package products to add to which target. Select the library you need and your app target, then click Add Package again.
Xcode downloads the source code, compiles it, and links it to your project. You’ll see the package appear under Package Dependencies in the project navigator.
Tip: You can also find packages by searching the Swift Package Index — a community-maintained catalog of thousands of open-source Swift packages.
Understanding Version Rules
When you add a package, Xcode asks you to choose a dependency rule that controls which versions of the package your project accepts. This matters because packages get updated over time, and you want updates that fix bugs without breaking your code.
There are three main options:
Up to Next Major Version (recommended for most cases) — Accepts any version from the one you specify up to (but not including) the next major release. For example, “Up to Next Major from 7.0.0” accepts 7.0.0, 7.1.0, 7.2.3, but NOT 8.0.0.
Up to Next Minor Version — More restrictive. “Up to Next Minor from 7.1.0” accepts 7.1.0, 7.1.1, 7.1.2, but NOT 7.2.0.
Exact Version — Locks to one specific version. This is the most restrictive and is generally not recommended because you’ll miss bug fixes and security patches.
Note: These rules follow Semantic Versioning (SemVer), a widely used convention:
- Major version (8.0.0) = breaking changes, your code might need updates
- Minor version (7.2.0) = new features, backwards compatible
- Patch version (7.1.3) = bug fixes only
Importing and Using a Package
Once a package is added, you use it by importing it at the top of any Swift file, just like you import SwiftUI or
Foundation.
import Kingfisher
import SwiftUI
struct MoviePosterView: View {
let posterURL: URL
var body: some View {
KFImage(posterURL)
.resizable()
.scaledToFit()
.frame(height: 300)
}
}
In this example, KFImage comes from the Kingfisher package. It downloads and caches the image from the URL
automatically — something that would take dozens of lines of code to build yourself.
Popular Packages Worth Knowing
As you start building iOS apps, you’ll encounter a few packages that the community uses widely. Here are some worth bookmarking:
| Package | What It Does |
|---|---|
| Kingfisher | Image downloading and caching |
| Alamofire | Networking (HTTP requests) |
| SwiftLint | Code style and convention linting |
| SnapKit | Auto Layout with a clean syntax |
| Lottie | Animated illustrations |
Tip: You don’t need to add packages for everything. Apple’s built-in frameworks cover a huge amount of functionality. Only add a package when it genuinely saves you significant time or solves a problem the standard libraries don’t address well.
Common Mistakes
Adding Packages You Don’t Need
Every package you add increases your app’s compile time and binary size. Before adding a package, check if Apple already provides the functionality you need.
For example, you don’t need a third-party JSON parsing library — Swift’s built-in Codable protocol handles JSON
encoding and decoding. And you don’t need a date formatting library — DateFormatter and Date.FormatStyle are part of
Foundation.
Not Checking Platform Compatibility
Some packages only support certain platforms or minimum iOS versions. Always check the package’s README or
Package.swift to verify it supports your deployment target before adding it.
Pinning to an Exact Version Indefinitely
While exact version pinning gives you stability, forgetting to update means you’ll miss important bug fixes and security patches. Prefer “Up to Next Major Version” and periodically review your dependencies for updates.
To update packages in Xcode, go to File → Packages → Update to Latest Package Versions.
What’s Next?
- Swift packages are reusable libraries you add to your project through Xcode
- Add them via File → Add Package Dependencies… with a repository URL
- Use “Up to Next Major Version” as your default version rule
- Import the package at the top of your Swift file to start using it
- Only add packages that provide genuine value — check Apple’s frameworks first
Ready to share your own code with the world? Head over to Creating Swift Packages to learn how to build, structure, and publish your own reusable library.