Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Swift Basics

Swift HOME Swift Intro Swift Get Started Swift Syntax Swift Statements Swift Output Swift Comments Swift Variables Swift Data Types Swift Type Casting Swift Operators Swift Strings Swift Arrays Swift Ranges Swift If...Else Swift Switch Swift While Loop Swift For Loop Swift Break/Continue Swift Collections

Swift Types & Functions

Swift Functions Swift Optionals Swift Enums & Patterns Swift Closures Tuples & Type Aliases

Swift Object Model

Swift OOP Swift Inheritance Swift Polymorphism Swift Protocols Swift Generics Swift Extensions Access Control Initializers Deinitializers Value Semantics & COW Equatable & Comparable

Swift Robustness & Async

Swift Error Handling Swift Concurrency Swift Memory

Swift Tooling

Swift Package Manager

SwiftUI Basics

SwiftUI Intro iOS Project Setup SwiftUI Layout SwiftUI Navigation SwiftUI Data Flow SwiftUI Lists & Forms SwiftUI Animations SwiftUI Gestures SwiftUI Modifiers & ViewBuilder SwiftUI Previews SwiftUI Accessibility SwiftUI Styling & Theming

SwiftUI Data & Architecture

Networking Persistence Persistence (Core Data) MVVM Architecture AppStorage & SceneStorage Testing SwiftUI

iOS Capabilities

Privacy & Permissions Push Notifications Widgets & Extensions Background Work Core Location App Clips Keychain Basics CloudKit File System Background URLSession MapKit

iOS Quality & Compliance

Localization Accessibility App Privacy In-App Purchases Analytics & Reporting Testing with XCTest

iOS Release & Distribution

Assets & App Icons Signing & Distribution TestFlight & App Store Ship Your First App

Swift Exercises

Swift Exercises Swift Quiz

Swift Package Manager (SPM)


Swift Package Manager (SPM)

Create packages, declare dependencies, and build libraries and executables with Swift Package Manager.


Create a New Package

Swift Package Manager (SPM) is Swift's built-in dependency manager and build tool.

Syntax: swift package init --type executable, swift build, swift run.

Example

$ mkdir hello-pkg && cd hello-pkg
$ swift package init --type executable
$ swift build
$ swift run
Hello, world!

This example initializes an executable package, builds it, and runs the generated binary.


Package.swift

The manifest file Package.swift describes your package, products, and dependencies.

Syntax: declare products, add dependencies, and list targets that build your code.

Example

// swift-tools-version: 5.10
import PackageDescription

let package = Package(
  name: "hello-pkg",
  products: [
    .executable(name: "hello-pkg", targets: ["hello-pkg"]),
  ],
  dependencies: [
    // .package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0")
  ],
  targets: [
    .executableTarget(
      name: "hello-pkg",
      dependencies: []
    )
  ]
)

This basic manifest declares an executable product and a single target with no external dependencies.


Targets, Products, Dependencies

Targets build code.

Products are what you ship (executables or libraries).

Dependencies are other packages you use.

Syntax: add .package(url:..., from: ...) in dependencies, and reference it via .product(name:..., package:...) in target dependencies.

Example: Add swift-argument-parser

// swift-tools-version: 5.10
import PackageDescription

let package = Package(
  name: "hello-pkg",
  products: [
    .executable(name: "hello-pkg", targets: ["hello-pkg"]),
  ],
  dependencies: [
    .package(url: "https://github.com/apple/swift-argument-parser", from: "1.2.0")
  ],
  targets: [
    .executableTarget(
      name: "hello-pkg",
      dependencies: [
        .product(name: "ArgumentParser", package: "swift-argument-parser")
      ]
    )
  ]
)
import ArgumentParser

@main
struct Hello: ParsableCommand {
  @Option(name: .shortAndLong, help: "Name to greet")
  var name: String = "Swift"

  mutating func run() throws {
    print("Hello, \(name)!")
  }
}
$ swift build
$ swift run hello-pkg --name W3Schools
Hello, W3Schools!

This example adds a dependency and links its product into the executable target, then builds and runs with an option.



Add a Dependency

Add a dependency in dependencies and link it in your target.

Then run swift build.

Syntax: swift package resolve to update dependencies and swift package clean to clean build artifacts.

Tip: Use swift package resolve to update dependencies and swift package clean to clean build artifacts.



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.