0Pricing
Swift Academy · Lesson

Package.swift: Targets, Products and Dependencies

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

Package.swift: Targets, Products and Dependencies is a free Swift Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

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

Yes — the full text of “Package.swift: Targets, Products and Dependencies” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

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