mobile-swift
Writes idiomatic modern Swift for iOS: Swift 6 strict concurrency (async/await, actors, @MainActor, Sendable, cancellation), value types, some vs any, typed throws, SwiftUI with @Observable, NavigationStack and stable identity, SwiftData, SPM modules and Swift Testing. Use when writing or refactoring Swift views, view models, services or packages, or fixing concurrency warnings and Swift 6 migration errors.
Swift for iOS
Write Swift the way a senior iOS engineer would in Swift 6: data-race safe by construction, value types first, no crashes on unexpected input, SwiftUI views that stay small and cheap, and code split into packages with tests next to it.
When to use
- Writing new Swift, SwiftUI or SwiftData code, or generating a feature from a ticket.
- Refactoring or migrating older code: completion handlers,
ObservableObject,NavigationView, Core Data, XCTest unit tests, Swift 5 language mode. - Fixing compiler errors such as "sending risks causing data races", "non-Sendable type", "main actor-isolated property can not be referenced".
- Adding a local Swift package, a test target, a lint config, or a privacy manifest.
Not for: reviewing someone else's diff (use mobile-code-review), a security audit (use
mobile-security), or choosing an app architecture (use mobile-mvvm, mobile-mvi).
Workflow
- Read the project settings before writing code.
- Swift language mode, strict concurrency level, default actor isolation, minimum iOS version.
Package.swiftfiles, existing lint and format configs, test framework in use.
- Match the deployment target.
@Observableand SwiftData need iOS 17,Mutexneeds iOS 18. Gate newer APIs withif #availableor ask before raising the target. - Load the reference for the area being touched: Concurrency, SwiftUI, SwiftData, Testing, Packages and tooling.
- Model data first: value types, explicit
Sendablewhere it crosses isolation, typed errors where the set is closed. - Decide isolation per type:
@MainActorfor UI-facing state, anactorfor shared mutable state,nonisolatedvalue types for everything else. - Write the code, then the tests in the same change. Swift Testing for unit tests, XCTest only for UI and performance tests.
- Build with zero concurrency warnings, run the tests, run SwiftLint and the formatter.
- List every
Info.plistkey, entitlement, background mode and privacy manifest entry the change adds. These have signing and App Review consequences and must be called out explicitly.
Rules
1. Structs by default, final class only when identity matters
// BAD: reference semantics for plain data, shared mutation, not Sendable
class Reading { var value: Double; var date: Date }
// GOOD
struct Reading: Sendable, Equatable { let value: Double; let date: Date }
// GOOD: identity and lifetime matter (connection, cache, model observed by UI)
@MainActor @Observable
final class DeviceSession { var state: ConnectionState = .idle }
- Classes are
finalunless subclassing is designed for. letovervar. Mutation happens through methods with clear names, not public setters.
2. No force unwraps, force casts or try! in app code
// BAD
let url = URL(string: base + path)!
let cell = tableView.dequeueReusableCell(withIdentifier: "c") as! DeviceCell
// GOOD
guard let url = URL(string: base + path) else { throw APIError.invalidURL(path) }
let name = device.name ?? String(localized: "Unknown device")
- In tests use
try #require(value)instead of!, so a nil fails the test instead of crashing the run. - A static literal URL may use a
guardwithpreconditionFailure("...")and a message, never a bare!.
3. some when one concrete type is fixed, any only when it must vary at runtime
// BAD: existential box and dynamic dispatch for no reason
func render(_ items: any Collection<Reading>) -> any View
// GOOD: generic, specialised by the compiler
func render(_ items: some Collection<Reading>) -> some View
// GOOD: heterogeneous storage really needs an existential
var sinks: [any TelemetrySink]
- Write
anyexplicitly for every existential even where the compiler does not yet require it. - Primary associated types (
some Collection<Reading>,any AsyncSequence<Int, Never>) instead of type-erasing wrappers.
4. Structured concurrency before unstructured Task {}
// BAD: unstructured tasks, no cancellation propagation, errors lost
Task { profile = try await api.profile() }
Task { devices = try await api.devices() }
// GOOD: both cancelled with the parent, errors propagate
async let profile = api.profile()
async let devices = api.devices()
state = .loaded(try await profile, try await devices)
- Dynamic fan-out uses
withThrowingTaskGroup; bound the width for large inputs. Task {}only at an entry point from synchronous code (button action, delegate callback), and its handle is stored and cancelled when the owner goes away.Task.detachedneeds a written reason.
5. UI-facing state is @MainActor, data crossing isolation is Sendable
// BAD: silences the checker, data race still possible
final class TokenStore: @unchecked Sendable { var token: String? }
// GOOD: an actor owns the mutable state
actor TokenStore {
private var token: String?
func update(_ new: String?) { token = new }
}
- View models:
@MainActor @Observable final class. @unchecked Sendableandnonisolated(unsafe)only with a lock guarding every field and a comment explaining why an actor does not fit.- No
DispatchQueue.main.asyncinasynccode; isolate the function or type to@MainActorinstead.
6. Actor state is re-checked after every await
// Inside an actor or a @MainActor class.
// BAD: reentrancy, two callers both pass the check and both connect
func connect() async throws {
guard state == .idle else { return }
try await transport.open()
state = .connected
}
// GOOD: claim the state before suspending
func connect() async throws {
guard state == .idle else { return }
state = .connecting
do { try await transport.open(); state = .connected }
catch { state = .idle; throw error }
}
7. Cancellation is checked in loops and forwarded to callback APIs
// BAD: keeps uploading after the user left the screen
for chunk in chunks { try await upload(chunk) }
// GOOD
for chunk in chunks {
try Task.checkCancellation()
try await upload(chunk)
}
- Wrap callback APIs that support cancelling in
withTaskCancellationHandler. Details in Concurrency. - Do not catch
CancellationErrorand turn it into a user-facing error state.
8. Callbacks are bridged once, at the edge
// BAD: continuation resumed for every discovery, traps on the second one
func scan() async -> Peripheral {
await withCheckedContinuation { c in
scanner.onDiscover = { c.resume(returning: $0) }
}
}
// GOOD: repeated events are a stream, cleaned up when the consumer stops
// scanner: a Sendable wrapper around the platform delegate API
func discoveries() -> AsyncStream<Peripheral> {
let (stream, continuation) = AsyncStream.makeStream(of: Peripheral.self)
scanner.onDiscover = { continuation.yield($0) }
continuation.onTermination = { [scanner] _ in scanner.stop() }
scanner.start()
return stream
}
- One-shot callback:
withCheckedThrowingContinuation, resumed exactly once on every path. - Repeated events:
AsyncStream/AsyncThrowingStreamwithonTermination.
9. Typed throws only for closed, local error domains
// BAD: public API promises a single error type forever, callers gain nothing
public func sync() async throws(SyncError)
// GOOD: closed set, callers switch exhaustively
func decode(_ frame: Data) throws(FrameError) -> Packet
enum FrameError: Error { case tooShort(Int), badChecksum, unknownOpcode(UInt8) }
- Use
throws(E)for parsers, validation and module-internal code. Use untypedthrowsat module and network boundaries where new failure kinds will appear. - Typed throws does not replace
Resultin stored properties.
10. SwiftUI: the view that creates a model owns it in @State
// BAD: legacy pattern, model recreated on every parent redraw
@ObservedObject var model = DeviceListModel()
// GOOD
struct DeviceListScreen: View {
@State private var model = DeviceListModel()
var body: some View {
DeviceList(model: model)
.task { await model.load() }
}
}
- Passed-in
@Observablemodels are plainlet/varproperties; use@Bindableonly to get bindings. @Bindingfor a value the parent owns.@Environmentfor app-wide dependencies..task(id: query)for work that restarts when an input changes; it cancels the previous run.
11. SwiftUI identity is stable and views stay small
// BAD: index identity, AnyView erasure, heavy body
ForEach(items.indices, id: \.self) { i in AnyView(row(items[i])) }
// GOOD
ForEach(items) { item in DeviceRow(device: item) }
- Items are
Identifiablewith an id from the data, neverUUID()created inbody. - Conditional content uses
@ViewBuilderorGroup, notAnyView. - Extract subviews when
bodystops fitting on a screen; no formatters, sorting or I/O insidebody.
12. Capture lists break cycles, and only where a cycle exists
// BAD: self -> task -> self, the loop keeps the object alive forever
monitor = Task { for await r in readings { self.append(r) } }
// GOOD
monitor = Task { [weak self] in
for await r in readings {
guard let self else { return }
append(r)
}
}
- Escaping closures stored by
self(timers, observers, stored tasks) capture[weak self]. unownedonly when the referenced object provably outlives the closure.- SwiftUI views are structs: no
[weak self]in.taskor button actions. - Delegates are
weak var delegate: (any SomeDelegate)?with anAnyObject-constrained protocol.
13. SwiftData contexts stay on their actor
// BAD: ModelContext and model objects are not Sendable
Task.detached { context.insert(entity) }
// GOOD: background work in a model actor, identifiers cross the boundary
@ModelActor
actor ReadingImporter {
func importAll(_ rows: [ReadingDTO]) throws {
for row in rows { modelContext.insert(ReadingEntity(row)) }
try modelContext.save()
}
}
- UI reads with
@Query; writes go through@Environment(\.modelContext)or a model actor. - Pass
PersistentIdentifierbetween actors, not model instances. See SwiftData.
14. Features are local packages with explicit, minimal APIs
// BAD: public only so a sibling target can call it, now visible to the whole app
public func makeDecoder() -> JSONDecoder
// GOOD: visible to other targets of the same Swift package, hidden from everyone else
package func makeDecoder() -> JSONDecoder
- One local package (or one target) per feature plus shared core modules; features depend on core, never on each other.
- Default to
internal.packagespans targets of one Swift package only;publicis for what other packages and the app target import. - Every dependency is declared in
Package.swift; nothing relies on a transitive import.
15. Swift Testing for unit tests, XCTest where it is still required
// BAD: a nil or a thrown error crashes the whole test run
func testParse() { XCTAssertEqual(try! parse(frame).battery!, 87) }
// GOOD: failures are reported, the run continues
@Test func parsesBatteryLevel() throws {
let packet = try parse(frame)
let battery = try #require(packet.battery)
#expect(battery == 87)
}
- XCTest stays for UI tests (
XCUIApplication) and performance tests (measure). - No
sleepin tests; inject a clock. Details in Testing.
Platform and project changes
Call these out in the change summary every time, with the exact key or entitlement name.
Info.plistusage strings:NSBluetoothAlwaysUsageDescription,NSCameraUsageDescription,NSLocationWhenInUseUsageDescriptionand others. For Bluetooth, camera and microphone a missing string terminates the app on first access.UIBackgroundModesentries such asbluetooth-centralorlocation. App Review asks for justification.- Entitlements (push, App Groups, Keychain sharing, associated domains) change the provisioning profile.
PrivacyInfo.xcprivacy: required-reason APIs (for exampleUserDefaults, file timestamps, system boot time, disk space) need a declared reason; collected data types and tracking domains must match reality. See Packages and tooling.
Anti-patterns
- Sprinkling
@MainActor,@unchecked Sendableornonisolated(unsafe)until the compiler is quiet. Task { }insideonAppearinstead of.task.DispatchSemaphoreorDispatchGroup.wait()to make async code synchronous.ObservableObjectand@Publishedin new code on iOS 17+.- Giant views with
AnyViewand computedsome Viewproperties instead of real subviews. UUID()as theidof items created during rendering.- Singletons (
static let shared) reached from deep inside views and models instead of injection. try?that silently drops an error the user should see.- One giant app target with every file
public"just in case". - Adding a third-party SDK without checking it ships its own privacy manifest and signature.
Checklist
- Language mode, strict concurrency and deployment target checked before writing
- Value types by default; classes are
final - No
!,as!,try!in app code; tests use#require - UI-facing types
@MainActor; shared mutable state in actors; no unjustified@unchecked Sendable - Actor state re-validated after each
await - Long loops check cancellation; continuations resume exactly once; streams have
onTermination - Escaping closures stored by
selfuse[weak self]; delegates areweak - SwiftUI models owned with
@State; async work in.task/.task(id:); stableForEachids - No
AnyViewor heavy work inbody - SwiftData contexts not shared across actors; identifiers passed instead of models
- New code in the right package with the narrowest access level
- Swift Testing unit tests added; XCTest only for UI and performance
- Zero warnings, tests pass, SwiftLint and formatter clean
-
Info.plist, entitlements, background modes and privacy manifest changes listed explicitly
Related skills
mobile-code-review-- review checklist that applies these rules to a diffmobile-security-- Keychain, ATS, data protection and MASVS checks for the code written heremobile-mvvm,mobile-mvi-- where view models, state and intents livemobile-design-patterns-- repository, coordinator and dependency injection patterns in Swiftmobile-ci-cd-- Xcode Cloud, signing and test plans in the pipelinemobile-kotlin,mobile-flutter-- the equivalent rules for Android and Fluttermobile-kmp-- consuming a Kotlin Multiplatform framework from Swiftmobile-ble-- CoreBluetooth rulesmobile-testing-- cross-platform test strategy and snapshot testsmobile-performance-- Instruments, launch time and MetricKit
- Downloads
- 62All time
- Security audit
- Pass
- Last updated
- 2d ago
- Current version
- v1.0.1
- License
- MIT-0
