Swift Academy · Lesson

Package.swift: Targets, Products and Dependencies

Anatomy of a Package.swift manifest and defining library and executable products.

Lesson 1 of 413 steps

Package.swift: Targets, Products and Dependencies is a free Swift Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

Package.swift Overview

Package.swift is the manifest for Swift Package Manager. It declares the package name, Swift version, targets, products, and dependencies.

import PackageDescription
let package = Package(
  name: "MyLibrary",
  platforms: [.iOS(.v16), .macOS(.v13)],
  products: [],
  dependencies: [],
  targets: []
)

Targets

Targets are the basic building blocks: .target (library/executable code) and .testTarget (test code).

.target(
  name: "MyFeature",
  dependencies: [],
  path: "Sources/MyFeature"
),
.testTarget(
  name: "MyFeatureTests",
  dependencies: ["MyFeature"],
  path: "Tests/MyFeatureTests"
)

Products

Products define what a package exposes to consumers: .library or .executable.

.library(
  name: "MyFeature",
  targets: ["MyFeature"]
),
.executable(
  name: "MyTool",
  targets: ["MyTool"]
)

Dynamic vs Static Libraries

Specify .dynamic or .static linking. Static is default and produces less overhead; dynamic enables sharing across modules.

.library(
  name: "MySDK",
  type: .dynamic,
  targets: ["MySDK"]
)

External Dependencies

Declare remote package dependencies with a URL and version requirement.

.package(url: "https://github.com/apple/swift-algorithms", from: "1.0.0"),
.package(url: "https://github.com/pointfreeco/swift-composable-architecture", exact: "1.0.0")

Using External Dependencies in Targets

Reference external package products in target dependencies using .product(name:package:).

.target(
  name: "MyApp",
  dependencies: [
    .product(name: "Algorithms", package: "swift-algorithms"),
    .product(name: "ComposableArchitecture", package: "swift-composable-architecture")
  ]
)

Local Package Dependencies

Reference a local package directory for monorepo setups or iterating on a package you own.

.package(path: "../MySharedLibrary"),

Swift Version Requirements

Set the minimum Swift tools version at the top of Package.swift and platform requirements in the package initializer.

// swift-tools-version: 5.9
import PackageDescription
let package = Package(
  name: "MyPackage",
  platforms: [.iOS(.v17)],
  // ...
)

Resources in Targets

Include non-code resources (images, JSON, localization) using the resources: parameter.

.target(
  name: "MyFeature",
  resources: [
    .process("Resources"),
    .copy("Fixtures/test_data.json")
  ]
)

Build Settings and C Targets

Targets can configure C flags and Swift settings, useful for wrapping C libraries or conditional compilation.

.target(
  name: "CWrapper",
  cSettings: [.headerSearchPath("include")],
  swiftSettings: [.define("DEBUG", .when(configuration: .debug))]
)

Package Resolution

Run swift package resolve to fetch dependencies. The resolved versions are stored in Package.resolved.

// Commands:
// swift package resolve      – fetch dependencies
// swift package update       – update to latest compatible versions
// swift build                – build the package
// swift test                 – run tests

Quick Check

Which Package.swift element defines what the package exposes to other packages or apps that depend on it?

Lesson Recap

Package.swift: targets are build units, products expose them to consumers, dependencies reference remote or local packages. Use .product(name:package:) to link external targets. Commit Package.resolved to lock versions in team projects.

Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “Package.swift: Targets, Products and Dependencies” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Package.swift: Targets, Products and Dependencies”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

What will I learn in “Package.swift: Targets, Products and Dependencies”?

Anatomy of a Package.swift manifest and defining library and executable products. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “Package.swift: Targets, Products and Dependencies” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Package.swift: Targets, Products and Dependencies
  2. Local Packages and Feature Modules
  3. Binary Targets and XCFrameworks
  4. Versioning, Resolving Conflicts and Swift Plugins
← Back to Swift Academy