Swift Package Manager Manifest File Tutorial: THAT Nobody Tells You About
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
[2] What is Package File?
Before starting, let’s see the project structure.
Now, you will see Package
file. That’s a super super important file. Let get into it.
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
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
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!
How to solve this? It’s easy. You need to specify supported platforms.
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
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/
Create Resources/
and Asset Catalog
. I added also Views/.
(2) Specify Resources
in Manifest
Now, you are all prepared.
[6] Code Time!
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
(2) Check URL
https://github.com/paigeshin/MySwiftPackage
Congratulations! You just have published your first library!
[7] Use your first Library!
(1) Add Packages
(2) Use your code!
import your swift package and use code!
[8] Common Workflow
Whenever you change source code in your swift package. Do this.
Regardless of local swift package or remote swift package(this example), your source will be updated immediately!
Other Relevant Subjects
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.