Pocket How
Data and architecture

Modularize an iOS app with local Swift packages

iOS 17+, iPadOS 17+. Intermediate. By Cihat Salik, .

Short answer

Put one local package next to the app project with a target per feature plus shared Core and UI targets. Features depend downward on Core, never on each other, and the app target composes them. Expose only what other modules need with public, use package for helpers shared inside the package, and load resources through Bundle.module.

The problem

A single app target works until it does not. Every change recompiles more than it should, previews take longer to load, any file can reach into any other, and nobody can tell where a feature ends. Local Swift packages fix that without new tools: the compiler enforces boundaries, Xcode rebuilds only the modules that changed, and each feature can be previewed and tested on its own. The cost is a little ceremony around access control and resources. This guide covers the layout, the rules that keep it healthy, and the traps.

Start with one package and many targets

You do not need one package per feature. Majid Jabrayilov’s microapps series starts with a single local package in the project’s root folder that holds many targets, shared by the app, its widgets and its watch app. Each target compiles as its own module, so Xcode only recompiles the modules that changed.

A layout that scales:

MyApp/
  MyApp.xcodeproj
  MyApp/                 app target: @main, composition, entitlements
  Packages/
    AppFeatures/
      Package.swift
      Sources/
        Core/            models, persistence, networking protocols
        DesignSystem/    colors, typography, shared controls
        LibraryFeature/  saved articles screens
        ReaderFeature/   article reader
        SettingsFeature/
      Tests/
        CoreTests/
        LibraryFeatureTests/

The manifest:

// swift-tools-version: 6.0
import PackageDescription

let package = Package(
    name: "AppFeatures",
    defaultLocalization: "en",
    platforms: [.iOS(.v17)],
    products: [
        .library(name: "LibraryFeature", targets: ["LibraryFeature"]),
        .library(name: "ReaderFeature", targets: ["ReaderFeature"]),
        .library(name: "SettingsFeature", targets: ["SettingsFeature"]),
        .library(name: "Core", targets: ["Core"]),
    ],
    targets: [
        .target(name: "Core"),
        .target(
            name: "DesignSystem",
            resources: [.process("Resources")]
        ),
        .target(name: "LibraryFeature", dependencies: ["Core", "DesignSystem"]),
        .target(name: "ReaderFeature", dependencies: ["Core", "DesignSystem"]),
        .target(name: "SettingsFeature", dependencies: ["Core", "DesignSystem"]),
        .testTarget(name: "CoreTests", dependencies: ["Core"]),
        .testTarget(name: "LibraryFeatureTests", dependencies: ["LibraryFeature"]),
    ]
)

With tools version 6.0, targets build in the Swift 6 language mode by default, so strict concurrency checking applies to every module from the start.

Add the package to the project with File > Add Package Dependencies > Add Local, then add the feature products to the app target’s Frameworks, Libraries, and Embedded Content.

Point dependencies one way

Modules only help if the graph stays a tree. The rule that holds up best in practice:

  • The app target depends on features. It owns @main, builds the real services, and wires features together.
  • Features depend on Core and DesignSystem. Never on each other.
  • Core depends on nothing in the app. No SwiftUI views, no feature types.

Dashlane’s write-up on how they organize their Swift packages uses the same three layers under different names: Foundation at the bottom, Core in the middle, Feature packages on top. Feature packages may import the two lower layers, never another feature. Their main lesson matches what most teams learn the hard way: a catch-all shared module grows into tangled code that is expensive to untangle later.

Jacob Bartlett’s Modular Architecture for Apps frames the same idea as one-directional data flow between layers. Top-level features depend on what data they need, behind abstractions, never on how networking or persistence get it. He also notes that a single Core module is often a step on the way, not the end state: once Core holds everything, every change to it recompiles every feature.

When two features need to talk

The reader screen opens from the library. If LibraryFeature imports ReaderFeature, you have broken the rule. Invert it: the library exposes a closure or a navigation value, and the app target fills it in.

import SwiftUI
import Core

public struct LibraryView: View {
    private let openArticle: (Article.ID) -> AnyView

    public init(openArticle: @escaping (Article.ID) -> AnyView) {
        self.openArticle = openArticle
    }

    public var body: some View {
        NavigationStack {
            ArticleList()
                .navigationDestination(for: Article.ID.self) { id in
                    openArticle(id)
                }
        }
    }
}

The app target composes it:

import SwiftUI
import LibraryFeature
import ReaderFeature

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            LibraryView { id in AnyView(ReaderView(articleID: id)) }
        }
    }
}

ArticleList and ReaderView are left out; they are ordinary views inside their features. If you prefer to avoid AnyView, make LibraryView generic over the destination view. The point is the same: only the app target knows both features.

Access control decides the API

Inside a module, internal is the default and you never think about it. Across modules, nothing is visible unless you mark it. Jabrayilov’s note is the one everyone trips on in the first hour: mark what other modules use as public.

Rules that keep the surface small:

  • public for the module’s API. The views the app creates, the models other modules read, the protocols they implement.
  • Write public initializers by hand. A struct’s memberwise initializer is internal even if the struct is public.
  • package for helpers shared inside the package. SE-0386 added the package access modifier in Swift 5.9. A package symbol is visible to other modules in the same package and invisible to the app target and anything outside. It is the right level for test helpers, shared formatters and internal protocols that features implement but the app should never touch.
  • internal for everything else.
public struct Article: Identifiable, Hashable, Sendable {
    public let id: UUID
    public var title: String
    package var cachedWordCount: Int?

    public init(id: UUID = UUID(), title: String) {
        self.id = id
        self.title = title
    }
}

If your SwiftData models live in Core, every property a predicate filters on must be visible to the module that builds the predicate. A #Predicate in LibraryFeature on Article.title needs title to be public or package.

Resources live in the module bundle

Packages can carry resources from tools version 5.3. Apple’s resource guide covers the details that matter:

  • Xcode recognizes asset catalogs, storyboards, XIBs, Core Data models and .lproj folders automatically. Everything else needs .process(_:) or .copy(_:) in the manifest.
  • Each target with resources gets its own bundle and an internal Bundle.module accessor. Always load through it. Apple’s guide says a package should not assume where its resources end up.
  • To let other modules use a resource, expose it through a public API rather than sharing the bundle.
import SwiftUI

public enum Palette {
    public static let accent = Color("Accent", bundle: .module)
    public static let card = Color("CardBackground", bundle: .module)
}

public extension Image {
    static let emptyLibrary = Image("EmptyLibrary", bundle: .module)
}

Forgetting bundle: .module is the most common bug in a freshly extracted module. The code compiles, the app looks fine because the asset still exists in the main bundle from before, and the image disappears the day you delete the old copy.

Localized strings follow the same rule. Apple’s localization guide requires defaultLocalization in the manifest when a package has localized resources, which is why the manifest above sets it. In SwiftUI, pass the bundle to text that comes from the package’s string table: Text("Saved articles", bundle: .module).

Build time: what you actually gain

The win comes from incremental builds. When you edit ReaderFeature, Xcode rebuilds that module and the app target, not LibraryFeature or SettingsFeature. Previews get faster for the same reason: a preview in a feature module only needs that module and its dependencies.

Three things eat the win:

  • A fat Core. If every feature imports a module that changes every day, every feature recompiles every day. Split Core by how often it changes: stable models and protocols in one module, frequently edited services in another.
  • Wide public APIs. A change to a public declaration forces dependents to recompile. Keep implementation details internal so edits stay inside the module.
  • Everything in one product. Keep feature products separate so extensions only link what they use. A widget that imports only Core and DesignSystem compiles and links less code.

Measure before and after with Xcode’s build timeline. Gains depend heavily on how the code is split, and a module boundary in the wrong place can make builds slower.

Traps

  • Cycles. Two features importing each other will not compile. Move the shared piece down into Core, or invert it with a closure as shown above.
  • Previews that need app setup. A preview in a feature module has no app delegate and no injected environment. Give each feature a preview helper that builds in-memory services and an in-memory ModelContainer.
  • Build phase scripts. Packages cannot run Xcode build phases. Dashlane moved code generation such as SwiftGen to Swift package plugins for that reason.
  • Too many packages too early. Start with one package and several targets. Split into more packages only when teams or release cycles differ.

Checklist

Step Done when
Create Packages/AppFeatures with Core, DesignSystem and one feature App builds, feature product linked
Move one screen into its feature target Its preview runs inside the package
Mark the API public, helpers package, rest internal App target sees only what it needs
Move assets into the module and use bundle: .module Old copies deleted from the app bundle, UI unchanged
Add defaultLocalization and move strings Strings load from Bundle.module
Invert feature-to-feature navigation No feature imports another
Check build timeline before and after Incremental build of one feature skips the others

Do the extraction one screen at a time, keep the app working after each step, and the dependency graph stays the one you designed.

Sources and further reading

Read next