skillfed

modern-swift

Modern Swift equips you with patterns for Swift 6.2's compiler-enforced concurrency model, replacing legacy approaches like completion handlers and DispatchQueue with async/await, actors for shared state, and Sendable constraints. Learn to enable strict concurrency checking, migrate existing code, and avoid common pitfalls like misused @unchecked Sendable and forgotten @MainActor annotations.

Modern Swift helps you write safe concurrent code using async/await, actors, and compile-time Sendable checking.

AI-generated summary based on this skill's SKILL.md

223 20 MIT updated by johnrogers

Install

johnrogers/claude-swift-engineering/modern-swift · repository language: Python

CLI (skillfed)coming soon
git clone https://github.com/johnrogers/claude-swift-engineering
cp -r claude-swift-engineering/plugins/swift-engineering/skills/modern-swift ~/.claude/skills/modern-swift

Frequently asked questions

AI-generated answers based on this skill's SKILL.md and metadata

How do I enable strict concurrency checking in my Swift package?

Modern Swift supports strict concurrency mode through your Package.swift configuration. Add `.upcomingFeature("StrictConcurrency")` to your target's swiftSettings, or use the `-strict-concurrency=complete` compiler flag. This enforces Sendable conformance and catches potential data races at compile time, aligning with Swift 6.2's compiler-enforced safety model.

What's the best way to migrate completion handlers to async await?

Modern Swift's migration path replaces callback-based APIs with async/await syntax. Convert completion handler parameters into return values or throws, wrap legacy callbacks using withCheckedThrowingContinuation, and update call sites to use await. This eliminates callback hell and makes error handling clearer while preparing code for strict concurrency checking.

How can I fix Sendable errors in Modern Swift?

Modern Swift requires types crossing actor boundaries to conform to Sendable. Ensure all captured variables are Sendable, use value types instead of classes where possible, and mark UI-bound types with @MainActor. Avoid @unchecked Sendable unless absolutely necessary; instead, refactor to use immutable data or proper actor isolation to satisfy the compiler's safety guarantees.

What is the actor pattern for managing shared mutable state?

Modern Swift's actor model isolates mutable state behind a single serial executor. Define an actor type, mark properties as private, and expose them through async methods. Actors prevent concurrent access automatically—the compiler enforces that only one task can modify state at a time, eliminating data races without manual locks.

How do I use async await in Swift 6 for concurrent operations?

Modern Swift's async/await syntax lets you write concurrent code that reads sequentially. Mark functions with async, use await at call sites, and leverage TaskGroup or async let for parallel work. This replaces DispatchQueue and completion handlers, making concurrency safer and more readable while enabling strict concurrency checking.

When should I use @MainActor for UI updates in Modern Swift?

Modern Swift requires all UI updates on the main thread. Annotate your view controller or view model class with @MainActor, or mark individual methods with @MainActor to enforce main-thread execution. The compiler prevents accidental background UI mutations, catching threading bugs early and integrating seamlessly with async/await and actor isolation.

SKILL.md

rendered from the published skill — quoted content, verbatim

Modern Swift (6.2+)

Swift 6.2 introduces strict compile-time concurrency checking with async/await, actors, and Sendable constraints that prevent data races at compile time instead of runtime. This is the foundation of safe concurrent Swift.

Overview

Modern Swift replaces older concurrency patterns (completion handlers, DispatchQueue, locks) with compiler-enforced safety. The core principle: if it compiles with strict concurrency enabled, it cannot have data races.

Quick Reference

Need Use NOT
Async operation async/await Completion handlers
Main thread work @MainActor DispatchQueue.main
Shared mutable state actor Locks, serial queues
Parallel tasks TaskGroup DispatchGroup
Thread safety Sendable @unchecked everywhere

Core Workflow

When writing async Swift code: 1. Mark async functions with async, call with await 2. Apply @MainActor to

(truncated - see the full file via the links below)

Read as markdown · JSON record · Browse the source repository

File tree — 9 files
plugins/swift-engineering/skills/modern-swift/SKILL.md
plugins/swift-engineering/skills/modern-swift/references/concurrency-essentials.md
plugins/swift-engineering/skills/modern-swift/references/macros.md
plugins/swift-engineering/skills/modern-swift/references/migration-patterns.md
plugins/swift-engineering/skills/modern-swift/references/modern-attributes.md
plugins/swift-engineering/skills/modern-swift/references/strict-concurrency.md
plugins/swift-engineering/skills/modern-swift/references/swift6-concurrency.md
plugins/swift-engineering/skills/modern-swift/references/task-cancellation.md
plugins/swift-engineering/skills/modern-swift/references/task-groups.md

Related skills

Tags

compile-time-safety actor-isolation cancellation-handling thread-safety-patterns legacy-modernization concurrent-programming sendable-constraints ui-threading