How to Build Swift Package

September 29, 2021 Publish By : EXPERT APP DEVS 2 min read Viewed By : 826
build swift package with xcode

What is a Swift Package Manager?

  • This is a tool for managing and distributing swift code.
  • It can integrate with swift build system of the auto process like downloading, compiling, and linking dependency
  • SPM is used easily to manage your project dependency.

Why use SPM?

  • SPM setup is very simple and easy.
  • This can also work on Linux.
  • SPM provides native solutions by apple.

Create iOS app

  • First, create an iOS app then you can see something like this:-

create iOS app

  • If you try to run this app it gives an error like no calculator.framework module.
  • Now we will create a swift package and link in our iOS app.

Swift Package in a Git repository

  • Create a new repository by following steps:- Open Xcode - Click on file - new - Swift package.
  • Give the name of your swift package and click on create.
  • Now open a terminal and run this command:- git init
  • Now you will see this:- 

git init

  • Now add code already we had in calculator.framework
  • So we will show something like this:-

calculator framework

  • Add new file XCTestManifests.swift and write the below code in this file

XCTestManifests swift

  • Now create a new file as LinuxMain.swift and write the below code in this file

LinuxMain swift

  • Now navigate to our CalculaterTest and add to the test it.

navigate to Calculater Test

  • Now our swift Package building is finished.
  • Let’s use it in our project.
  • Go to your iOS app Click on file - swift packages - Add package dependency and type git repository URL.

git repository URL

  • Now click on next and choose anyone from three options ( Version, Branch or commit)
  • You will see below screen:-

Version - Branch or commit

  • Now click on finish then Xcode will finish resolving and fetching the package dependency.
  • Once you click on the finish you will see dependency on the left side of your project.
  • If you want to change the package requirement you need to select a project and click on the swift package, then double click on the package you want to change.

change the package requirement

Need a consultation?

Drop us a line! We are here to answer your questions 24/7.