Swift Package Manager Manifest File Tutorial: THAT Nobody Tells You About

KD Knowledge Diet
5 min readApr 17, 2022

Every tutorial found online is talking about how to install dependencies through Swift Package Manager. Damn! Even a kid without any prior programming knowledge would understand it! Why is it so rare to find articles talking about SPM Manifest file? So I will rather talk about how to create your own package with Swift Package Manager, as well as how to specify Swift Manifest File. This knowledge acquired will help you a lot in writing more efficient code through modularization.

[1] Create Swift Package

Create Swift Package

[2] What is Package File?

Before starting, let’s see the project structure.

Swift Manifest File

Now, you will see Package file. That’s a super super important file. Let get into it.

Package.swift

This Package.swift contains all information about your package. You can add library, assets, and specify source code and resources as well.

[3] Install Third Party Libraries

As an example, I will use the most popular library Alamofire.

(1) Go to git repository

https://github.com/Alamofire/Alamofire

If you find a library written in Swift, almost all libraries support Swift Package Manager. There, you can find url to install dependencies in your Swift Package.

(2) Specify Dependency

Add dependencies
Alamofire added

Not only should you specify url, but you need to add Alamofire, inside target. Otherwise, your swift package manager won’t find your dependency. After adding these, build your swift package with command + b. Now your swift package starts to install Alamofire.

(3) Oh there’s a version issue!

Alamofire requires iOS above 10.

How to solve this? It’s easy. You need to specify supported platforms.

ios above 14, macOS above 11, watchOS above 8

By adding key ‘platforms’ and elements with platforms, you can define what platforms you want to support. Now, error has gone!

[4] Specify where your sources will be

Define Path

By defining path, You can decide which source will be used as a package. Currently, all source in Sources/ will be of use.

[5] Adding Assets

(1) Create Asset Catalog in Sources/Resources/

Add Asset Catalog
Folder Structure

Create Resources/ and Asset Catalog. I added also Views/.

(2) Specify Resources in Manifest

Specify Asset you created

Now, you are all prepared.

[6] Code Time!

Just a simple view

Nothing special. But whenever you write code, just make sure everything should be public. Otherwise, other packages can’t access your source.

When using Swift Package image file, you need to specify Image with UIImage(named: “imageName”, in: .module, with nil), if not, your module couldn’t find the image inside module.

[6] Moment of truth

(1) Create Git Repository

Create Repository
Created

(2) Check URL

URL created by github

https://github.com/paigeshin/MySwiftPackage

Congratulations! You just have published your first library!

[7] Use your first Library!

(1) Add Packages

Add Packages
type your repository URL

(2) Use your code!

Perfect!

import your swift package and use code!

[8] Common Workflow

Whenever you change source code in your swift package. Do this.

Update to Latest Package Versions

Regardless of local swift package or remote swift package(this example), your source will be updated immediately!

Source

Conclusion

  • You can specify your source path, resource path, and add packages in manifest file.
  • When writing code in Swift Package, don’t forget to use public access modifier. (Especially, initializer)
  • Whenever you change source code in swift package, use xcode’s Update to Latest Package Versions features.

--

--

KD Knowledge Diet

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!