Swift Package Manager(SPM) Part 3

https://swift.org/package-manager/

It is mandatory to see previous part, otherwise you don’t get anything. In previous part we create swift package and used it in other app. In this blog we will see how we can edit out package

Editing Packages

  1. Both local and remote package are editable , We will see later what I mean by remote package are editable
  2. Package added as dependency of project lock for editing because they are auto managed by Xcode,

As you can see in Figure 1 , our App is pointing to private Home package repo through SPM remotely

Figure 1

As shown in Figure 2, I can add break point, print variable name in package itself. but, you can’t directly edit a Swift package you add as a dependency to an Xcode project as I discussed in point 2. It’s read-only and managed by Xcode. What you can do is add a local copy of the package that overrides the package dependency.

Figure 2

First step is to clone the package,

Figure 3

Make sure if you open package in Xcode close that project , Now Drag package to the project and build the project

Gif 1

As shown in Figure 4, we are now pointing to local package, If we checkout package standalone and add it our project as a local package, it will override existing dependency without removing it as shown in Figure 4 no Home package in Swift Package Dependency.

Figure 4

Our project didn’t remove remote Home package configuration from project, but currently it is pointing to local package, it just override existing with local package, but Xcode is smart enough knows it’s local package

Note: adding a local package overrides the package dependency with the same name. Xcode uses the package dependency again when you remove the local package from your project.This overriding cal also be done with packages you don’t own like Alamofire in our case , if you need to fix bugs or make as small change of the bug i n your own problem

Figure 5

As shown in Figure 6, we added new method using local editing in Home package

Figure 6

Since local package reference to the clone Home package repo , you can see the diff in source control, commit and push and add new release 1.1.0 since it’s a minor change

Figure 7
Figure 8

Now since I am done with the changes , I can remove local package and point to remote version of Home module . Right Click on Local Home package → Delete → Remove reference (No move to Trash , it will delete the Home repo as well since we are referencing to repo locally)

Figure 9

Build the appliccation, now you can see our project is now pointintg to remotely but the version is older which is 1.0.0

Figure 10
Figure 11

Now our project is pointing to latest Home module version

Figure 12

we can open the clone Home package in Xcode as well, doucble click on Package.swift

Figure 13

If you see the run destination of our Home package , you can see the package available for all platform out of the box. And you might’ve also noticed that we did not have to configure anything explicitly about platforms.This is because packages are platform-independent in nature. A swift package are always platform independent , if your package supports multiple platform and you have some platform specific code , you can use swift conditional features like in Figure 14.2

Figure 14.1
Figure 14.2

Edit Package Remotely

As shown in Figure 15 , we edit package directly on clone repo, Commit → Push → Draft new release 1.2.0 → Publish Release

Figure 15

Now I need to update my SPM package , Let’s say If I delete the derived data

Figure 16

It will delete all SPM fetched repo, because package is downloaded into the DerivedData when it’s resolved. If you completely nuke your DerivedData/ directory and then attempt to build your project, it will fail to build with the error “Missing package product” for each package. To trigger re-downloading, you must choose File > Swift Packages > Resolve Package Versions. What a pain this would be if you were working without an Internet connection.

Figure 17

Useful Links

https://www.jessesquires.com/blog/2020/03/04/another-issue-with-swiftpm-xcode-integration/

Senior iOS Engineer | HungerStation | Delivery Hero