DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Deep Q-Learning Networks: Bridging the Gap from Virtual Games to Real-World Applications
  • How To Use an Automatic Sequence Diagram Generator
  • Rule-Based Prompts: How To Streamline Error Handling and Boost Team Efficiency With ChatGPT
  • An Overview of Cloud Cryptography

Trending

  • Deep Q-Learning Networks: Bridging the Gap from Virtual Games to Real-World Applications
  • How To Use an Automatic Sequence Diagram Generator
  • Rule-Based Prompts: How To Streamline Error Handling and Boost Team Efficiency With ChatGPT
  • An Overview of Cloud Cryptography
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Swift Package Manager, Part 1: Introduction to Swift Package Manager

Swift Package Manager, Part 1: Introduction to Swift Package Manager

In the first in this series on the Swift package manager, we take a look at how to use it, create a package, and examine the architecture.

Yogesh Bharate user avatar by
Yogesh Bharate
·
Updated Aug. 07, 20 · Tutorial
Like (3)
Save
Tweet
Share
14.49K Views

Join the DZone community and get the full member experience.

Join For Free

What Is Swift Package Manager?

The Swift Package Manager is a tool for managing the distribution of Swift code.

It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.

The Package Manager is included in Swift 3.0 and above.

Using the Package Manager

Swift package manager provides a convention-based system for building libraries and executables and sharing code across different packages.

Shell
 




x


 
1
$ swift package --help
2
OVERVIEW: Perform operations on Swift packages
3
...



This command gives you the details command available for Swift Package Manager.

Creating a Package

1. Create a Package name directory, using below command

Shell
 




xxxxxxxxxx
1


 
1
$ mkdir NetworkManager
2
$ cd NetworkManager
3

          
4
// This command is use to create the NetworkManager directory.



2. Initialize the Swift Package Manager

Shell
 




xxxxxxxxxx
1


 
1
$ swift package init



Every package must have a manifest file called Package.swift in its root directory.
Package.swift

OR

Create executable type using below command:

Shell
 




xxxxxxxxxx
1


 
1
$ swift package init --type executable



A target is considered as an executable if it contains a file named main.swift. The package manager will compile that file into a binary executable.

3. Compile Swift Package

Shell
 




xxxxxxxxxx
1


 
1
$ swift build



4. Running executable framework

Shell
 




xxxxxxxxxx
1


 
1
$ swift run framework



NOTE - In Xcode 11, we can also perform the above setup using the File > New > Swift Package menu command.

Package Directory Structure

By default, the swift package init command creates the below package directory structure.

Package directory structure

Configure Swift Package

Swift packages don’t use .xcproject or .xcworkspace but rely on the folder structure and use the Package.swift file for additional configuration.

The following code listing shows a simple package manifest:
Simple package manifest

A Package.swift the file needs to begin with the string // swift-tools-version, followed by a version number such as // swift-tools-version:5.1.

The swift-tools-version declares the minimum version of Swift required to build this package.

Add Code to the Swift Package

You can add a source file to a package by dragging it into the Project navigator or by using the File > Add Files to [PackageName] menu.

Note - A Swift package’s targets can contain Swift, Objective-C/C++, or C/C++ code, but an individual target can’t mix Swift with C-family languages.

For example, a Swift package can have two targets, one that contains Objective-C, Objective-C++, and C code, and a second one that contains Swift code.

Adding Remote Dependencies

Besides facilitating the creation of packages, one of the Swift Package Manager’s core use cases is enabling remote dependencies — such as third party libraries — to be added to a project. Any package that can be fetched through Git can be added simply by specifying its URL, as well as the version constraint that we wish to apply to it:

Using tag version:


Using tag version

Using the exact tag version:

Using exact tag version

Using branch name:

Using branch name

Using the exact commit:

Using exact commit

Using local repository:Local repository
Thanks for reading this tutorial and in the next part, you will learn more about how to publish the Swift Package Manager with Xcode and how to add package dependency in the project.

Swift (programming language) Package manager

Opinions expressed by DZone contributors are their own.

Trending

  • Deep Q-Learning Networks: Bridging the Gap from Virtual Games to Real-World Applications
  • How To Use an Automatic Sequence Diagram Generator
  • Rule-Based Prompts: How To Streamline Error Handling and Boost Team Efficiency With ChatGPT
  • An Overview of Cloud Cryptography

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: