Creating Swift Package - A Step-by-Step Guide Part-1

Discover how to enhance code modularity, facilitate code sharing, and streamline project dependencies using Swift Package Manager.

Suji Raj. A
4 min readSep 22, 2023

SujiRaj. A - Man Power Makes Everything.

We all know Swift package manager is a powerful tool to share/reuse the Swift code. In this blog, we will see a small intro about Swift package manager and in detail about Swift packages.

What is a swift package manager?

Apple document: The Swift Package Manager or SPM 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 Swift package manager should be included in Swift 3.0 and other upgraded versions.
  • The mentioned Swift package module code which resolves a particular problem can also be reused in any other project.

Wow… Swift package helps to distribute one common code to all projects, to reuse which will increase our productivity.

Yeah enough with the wait time, let’s see the options here

  1. By Just using 5 simple commands in the terminal, we can create a simple swift package.
  2. By adding a few words and a few button clicks we can create a simple swift package.

Before creating the swift package here are some insights.

Module: Swift can organize multiple lines of code into a single model. Each module has a specific namespace and access level control, for which code can be accessed from outside the module or project.

A module inculcates all its code in a single module or it may depend on or import another module.

A module can be reusable to solve any problem.

You can define all of your code into a single module or break it up into multiple modules.

Packages: The package file is the manifest file. We need to define all the package names, description, and as well as their dependencies

Products: In products, the target is a library or an executable product.

executable: Executable files can be run by an operating system such as a web server.

library: A library is a simple collection of files.

A library contains a module that can be imported by any Swift project.

Dependencies: We will be able to import one module into another module to reuse the code by adding a URL like git or bitbucket. we can be able to add a version to download the particular content of the module or library.

Now it’s time to create a swift package via Mac terminal

To Create the folder for the Swift package, use the command below to create a folder.

%> mkdir <Your package name>

Image 1

Now we need a folder to create a package, for that purpose first navigate to the folder using the cd command.

Image 2

Now we have the init command for the package to create some default files automatically. Refer to the below code for it.

%> swift package init — -type=library

Now you can see some default folder structure and files have been created.

Image 3

Hooray!!! Finally, we have created a swift package…

Now it’s time to check if the files have been created without any error.

So, we can use the command below to check

%> swift build

After this command, the results will look like the image 4.

Image 4

Now we can open our swift package in xCode to write our own module to build a new swift package.

Image 5
public class SamplePackage {
// Here we can write our own package code
}

We did it. We have finally created the package.

Guys, To learn more about the addition of Swift Package into the Xcode, Wait for my second article to knock onto your system.

In my upcoming article, you can see how we can add a swift package to the Xcode project.

--

--