Local SPM — Mastering Modularization with Swift Package Manager (Xcode 15.+)

Guy Cohen
3 min readMar 13, 2024

--

In the realm of iOS development, project modularization is more than a trend — it’s a strategic approach that significantly enhances the maintainability, scalability, and efficiency of your codebase. This article delves into the “Past,” “Why,” and “How” of project modularization, leveraging Swift Package Manager (SPM) to achieve a structured, modular architecture.

If you’re already familiar with the reasons for modularization, feel free to jump ahead to the “How” section to dive into the practical steps of implementing it in your projects.

The initiative to modularize an iOS project, utilizing Swift Package Manager (SPM) to craft a more structured, scalable, and maintainable codebase, has been insightfully introduced and explored by the team at PointFree.co. For those eager to delve deeper into this topic, PointFree’s discussions on modularization offer an invaluable foundation: Modularization Part 1 and Modularization Part 2. It’s important to acknowledge, though, that the process for adding Swift packages has seen updates, especially with the advent of Xcode 15. This article is intended to complement those resources, providing an up-to-date guide for effectively modularizing your iOS projects using the latest tools and methodologies.

The Past

Before Swift Package Manager’s advent, developers relied on various methods to manage libraries and dependencies in their Swift projects. One common approach was using FrameworkKit — a generic term for the use of frameworks and libraries that needed to be manually integrated into projects. This process, while functional, had several challenges and limitations, which are beyond the scope of this discussion.

The Why?

Modularization refers to the practice of dividing a software project into distinct modules, each encapsulating a set of related functionalities. This architectural strategy offers numerous benefits:

  • Improved Build Times: By compartmentalizing your code into modules, you can significantly reduce build times, as only the changed modules need to be recompiled.
  • Enhanced Code Quality and Maintainability: Modules enforce strong boundaries, promoting encapsulation and separation of concerns. This makes your codebase easier to understand, maintain, and evolve.
  • Isolation for Testing and Development: Modules allow developers to work on isolated parts of the application without the need for the entire codebase, facilitating easier testing and faster development cycles.
  • Reusability: Modular code can be easily reused across different parts of the application or even in other projects, reducing redundancy and development effort.

The How?

Go to “File” -> “New” -> “Package”.

Select “Library” and then click “Next”.

Be cautious at this stage:

  • First, set the “Save As” with a meaningful name. It can be simple, like Package or Libraries, or something more specific, like the prefix of your project.
  • Second, it’s highly recommended to create a new subfolder with the same name as your file.
  • Lastly, choose your project target in the “Add to” and “Group” sections.

In the following example, I chose to save it as Libraries and created a Libraries folder.

Now, you should have the Package.swift file, which in this example, should be inside the Libraries folder.

This concludes Part 1. Stay tuned for Part 2, where I’ll show you how to efficiently organize the Package.swift file.

--

--