The Power of Local Swift Packages: Simplifying Feature Development in Your iOS Project

Sunil Gurung
4 min readAug 25, 2024

--

As iOS developers, we’re all familiar with the challenges that come with maintaining and scaling a growing codebase. It’s easy for things to get messy when you’re juggling multiple features, dependencies, and updates. But what if there was a way to keep everything neat, organized, and easy to manage? Enter local Swift Package Manager (SPM) modules — a strategy that can revolutionize how you structure your iOS projects.

In this article, we’ll explore why breaking down your project into local SPMs for each feature or flow isn’t just beneficial, it’s transformative. Plus, I’ll walk you through how to set this up in your own projects.

Why Use Local SPM for Each Feature or Flow?

1. Modularity that Makes Sense
Imagine each feature in your app as its own mini-project. By encapsulating each feature or flow in a separate SPM module, you can create a clear, modular structure. This makes your codebase easier to understand and navigate, especially as your project grows. You won’t have to dig through layers of unrelated code to find what you’re looking for — everything related to a specific feature is contained within its own module.

2. Maintenance Made Easy
When every feature has its own module, making updates or fixing bugs becomes far less risky. You can work on one feature without worrying about unintentionally breaking another. This isolation also means that when you revisit a module months later, it’s easier to jump back in because the scope is limited to that specific feature or flow.

3. Teamwork Without Tangles
In team environments, local SPMs can be a lifesaver. Different team members can work on different modules simultaneously, without the fear of merge conflicts or dependency issues. This setup promotes parallel development and speeds up the overall process, making your team more efficient.

4. Reusability Across Projects
Let’s say you’ve built a robust user authentication system in one project. With local SPMs, you can easily reuse that module in another project without extra work. This kind of code reusability not only saves time but also ensures consistency across different projects.

5. Focused Testing
Testing each module independently ensures that every part of your app works as expected before it’s integrated into the larger project. This isolated testing approach helps catch issues early, leading to fewer bugs in the final product and more reliable software.

6. Organised and Scalable Design
A well-organised project is a scalable project. By keeping your code structured through local SPMs, you create a design that’s easy to scale. Whether you’re adding new features or onboarding new developers, your project remains easy to navigate, understand, and extend.

How to Create a Local SPM in Your iOS Project

Now that you see the benefits, let’s get into how you can create a local SPM in your own project.

Step 1: Set Up Your Swift Package
1. Open Xcode and go to `File > New > Swift Package`.
2. Give your package a name that reflects its purpose, such as `UserAuthentication`.
3. Choose a directory within your project where you want to store the package — typically a `Packages` folder.

Step 2: Configure Your Package
1. Xcode will create a `Package.swift` file. This file is the heart of your package, defining its dependencies, targets, and other configurations.
2. Add your source files to the `Sources` directory and your test files to the `Tests` directory.
3. Update the `Package.swift` to reflect your module’s structure. Here’s an example for a user authentication module:

// swift-tools-version:5.9

import PackageDescription
let package = Package(
name: "UserAuthentication",
platforms: [
.iOS(.v15)
],
products: [
.library(
name: "UserAuthentication",
targets: ["UserAuthentication"]),
],
dependencies: [
// Add any external dependencies here
],
targets: [
.target(
name: "UserAuthentication",
dependencies: []),
.testTarget(
name: "UserAuthenticationTests",
dependencies: ["UserAuthentication"]),
]
)

Step 3: Add Your Local SPM to Your Project
1. In your main project, go to `File > Add Packages`.
2. Click `Add Local…`, navigate to your `Packages` directory, and select the package you just created.
3. Add the package to your project, and you’re ready to use it.

Step 4: Use Your Package
1. Import your package in the files where you need it:

import UserAuthentication

2. Start using the module’s functionality within your project just like any other Swift package.

Conclusion

By organising your iOS project into local SPMs, you’re not just tidying up your codebase — you’re setting the stage for better maintenance, collaboration, and scalability. It’s a simple strategy that can make a big difference in how you develop and manage your apps.

So, next time you’re starting a new feature, consider encapsulating it in its own Swift package. Your future self — and your teammates — will thank you.

--

--