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.

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

Creating a Package

1. Create a Package name directory, using below command

2. Initialize the Swift Package Manager

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

OR

Create executable type using below command:

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

4. Running executable 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.



Source link

Write A Comment