A migration plan that works
Turning on Swift 6 for a whole app at once is like renovating every room of a house on the same day: nothing works until everything works. Instead, go room by room:
- One module at a time, starting with the ones that don’t depend on others (models, networking). In a single-target app, start with the checks as warnings.
- Turn on complete checking as warnings first: the “Strict Concurrency Checking: Complete” build setting while still in Swift 5 mode. The app still builds; you see every problem.
- Fix the warnings with the patterns in the next part.
- Switch that module to Swift 6. Now the same problems are errors, so they can’t come back.
Before fixing anything, decide where code should run, its isolation domainIsolation domainIn Swift concurrency, the region that owns some mutable state: an actor, the main actor, or nonisolated code.Open in glossary →. Most app code is UI or view models, so it belongs on the main actor. Shared data that many tasks touch goes in an actor. Plain data (structs of strings and numbers) is Sendable and can go anywhere.
Migrate per module, bottom-up: dependencies first, so upstream modules don’t have to absorb downstream annotations later. In each module, enable StrictConcurrency (complete checking) as warnings in the Swift 5 mode, drive warnings to zero, then set the Swift 6 language mode, which makes them errors and enables the dynamic isolation checks. Settle the isolation design first, since it determines most fixes: UI types and view models @MainActor (in Swift 6.2, defaultIsolation(MainActor.self) / “Default Actor Isolation” makes this implicit for app and UI modules, with nonisolated as the visible opt-out); shared mutable state in actors; domain values Sendable structs; CPU-heavy pure work nonisolated (with @concurrent under NonisolatedNonsendingByDefault). Use @preconcurrency import for dependencies that predate annotations (it downgrades their Sendable diagnostics; remove it when they update), and treat every @unchecked Sendable and nonisolated(unsafe) as a code-review item with a justification comment. Track progress as warnings per module in CI so the count only goes down.
// Package.swift (swift-tools-version: 6.2): per-target language modes and checks
.target(
name: "Networking",
swiftSettings: [.swiftLanguageMode(.v6)] // done: errors, no going back
),
.target(
name: "ProfileFeature",
dependencies: ["Networking", "DesignSystem"],
swiftSettings: [
.swiftLanguageMode(.v5), // still migrating
.enableUpcomingFeature("StrictConcurrency"), // complete checking, as warnings
.defaultIsolation(MainActor.self), // UI module: main actor unless stated
]
),// A dependency that hasn't adopted Sendable yet: suppress its diagnostics, not yours
@preconcurrency import LegacyAnalyticsSDKswiftLanguageMode(.v6)marks a module as finished: the compiler now rejects code that could race.StrictConcurrencyin Swift 5 mode shows the same problems as warnings, so the module keeps building while you fix them.defaultIsolation(MainActor.self)(Swift 6.2) makes everything in the module run on the main actor unless marked otherwise, which is what UI code wants anyway. In Xcode it’s the Default Actor Isolation build setting.@preconcurrency importquiets warnings that come from an older library you can’t change.
Tap the line with the bug.
final class ImageCache: @unchecked Sendable {
static let shared = ImageCache()
private var images: [URL: UIImage] = [:]
func image(for url: URL) -> UIImage? { images[url] }
func store(_ image: UIImage, for url: URL) { images[url] = image }
}Hint: What does @unchecked promise, and does this class keep the promise?
@unchecked Sendable tells the compiler “trust me, this type is safe to use from many threads”. It isn’t: two download tasks calling store at once write to the same dictionary, which is exactly the data race Swift 6 exists to prevent. The error is gone, and the crash is still there.
Make the type actually safe. An actor is the simplest:
actor ImageCache {
static let shared = ImageCache()
private var images: [URL: UIImage] = [:]
func image(for url: URL) -> UIImage? { images[url] }
func store(_ image: UIImage, for url: URL) { images[url] = image }
}
// Callers now await: let image = await ImageCache.shared.image(for: url)Practice: plan the migration of a small app
A weather app has four parts: Models (structs), WeatherAPI (a class with a URLSession), LocationService (a class wrapping CLLocationManager) and the app target with SwiftUI views and view models.
- Models first: structs of
String,DoubleandDateareSendableautomatically if they’re not public; public ones need: Sendablewritten out. Switch to Swift 6. - WeatherAPI next: if it only holds
let session: URLSessionand aletbase URL, make it afinal class(or astruct) that isSendable. Switch to Swift 6. - LocationService: it receives delegate callbacks, so it needs the patterns from the next part. Warnings first, then fix.
- App target last: view models become
@MainActor(or use the default-isolation setting), then switch to Swift 6.
Choosing isolation per type
| Type | Isolation |
|---|---|
| Views, view models, coordinators, UI services | @MainActor (or module default) |
| Caches, stores, token managers shared across tasks | actor |
| Models, DTOs, configuration | Sendable structs and enums |
Stateless clients holding Sendable dependencies | Sendable final class or struct |
| CPU-heavy pure functions | nonisolated, @concurrent when it must leave the caller’s actor |
| Legacy classes with a lock you trust | @unchecked Sendable + Mutex (iOS 18) with a comment, temporary |
Mutex from the Synchronization module is Sendable and protects state without an actor’s async hop, which suits small synchronous critical sections.
- Bottom-up per module: complete checking as warnings in Swift 5 mode → zero → Swift 6 mode. Track warnings per module in CI.
- Isolation design first: UI on
@MainActor(Swift 6.2defaultIsolation(MainActor.self)), shared state in actors, dataSendable, CPU worknonisolated/@concurrent. @preconcurrency importfor un-annotated dependencies; remove later.@unchecked Sendableandnonisolated(unsafe)need a real synchronization story (Mutex, serial queue) and a review comment.
Check this part
What’s a safe first step for a big app moving to Swift 6?
Where should a view model usually run?
What does @preconcurrency import change?
A class with a mutable dictionary is marked @unchecked Sendable to silence errors. Result?