The Swift Package Manager (SPM) helps developers modularize code, improve reusability, and streamline dependency management using Apple's preferred tool. Many iOS developers are still transitioning from CocoaPods and Carthage, making a clear guide on creating and integrating SPM packages highly relevant. Additionally, SPM encourages open-source contributions, enhances team collaboration, and improves build times by promoting a more structured development approach.
In this post, we will implement a simple SPM package that generates a random dice value. Later, we will integrate it into an iOS app that displays the dice value.
Dice SPM
The first step is to create and navigate into the folder that will contain the SPM package implementation. Use the following command to create the library scaffolding.
swift package init --type library

Open project with XCode.
xed .
This is folder structure for the project:

This is what it does our SPM:
public struct DiceSPM {
public static func roll() -> String {
let values = ["Ace", "J", "K", "Q", "Red", "Black"]
return values[Int.random(in: 0...(values.count - 1))]
}
}
And its tests:
@Test func example() async throws {
var dicValues: [String: Int] = ["Ace": 0, "J": 0, "K": 0, "Q": 0, "Red": 0, "Black": 0]
for _ in 0..<100 {
let result: String = DiceSPM.roll()
dicValues[result]! += 1
}
for value in dicValues.keys {
#expect(dicValues[value] ?? 0 > 0)
}
}
Build and run tests:

Create an new GitHub public repository and upload all generated stuff:

Last but not least, documenting the README.md file is always a good practice for regular source code, but for libraries (such as SPMs), it is a MUST.

You can find SPM hosted in following GitHub repository.
Dice Consumer
DiceConsumer will be a simple app that retrieves values from the DiceSPM package. The first step is to import the SPM package.

And just call SPM library implementation from View:
import SwiftUI
import DiceSPM
struct ContentView: View {
@State private var dice: String?
var body: some View {
VStack {
if let dice = dice {
Text(dice)
.font(.largeTitle)
}
Button {
dice = DiceSPM.roll()
} label: {
Text("Roll the dice!")
}
}
.padding()
}
Finally build and deploy on simulator:

Conclusions
CocoaPods is no longer maintained, and Swift Package Manager (SPM) was intended to replace it and has now successfully succeeded it. In this post, I have demonstrated how easy it is to publish an SPM package and import it into any project.
You can find source code used for writing this post in following repository.