/officialunofficial-skills:write-swiftfrom Official Unofficial Skills
Guides modern Swift through 6.3 with value types, data-race safety, concurrency model (main actor, @concurrent, actors, task groups), some/any, API design, ARC, Swift Testing, macros. Use when writing, reviewing, or migrating Swift, or fixing concurrency errors, hangs, data races, retain cycles, or performance problems.
Ships in Official Unofficial Skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Start with the simplest, most static, most single-threaded code that works. Add concurrency, reference semantics, existentials or unsafe pointers only where you can state the reason. Every rule below applies that one idea.
The baseline is Swift 6.3. Items marked "6.4" need a Swift 6.4 toolchain, so check the project toolchain before you write one. The concurrency rules assume the Swift 6.2 model. On Swift 6.1 or earlier, the rules about async and @concurrent do not apply.
Use the first column. Move to the right only for the stated reason.
| Need | Default | Move on only when |
|---|---|---|
| Data | struct or enum | you need identity, sharing or inheritance |
| Abstraction | a concrete type | code repeats across types |
| Polymorphism | some P | you need mixed storage, then any P |
| Execution | main actor, synchronous | a profile shows a hang: async, then @concurrent, then actor |
| Memory | Array, String | a profile shows the cost: InlineArray, Span |
| Safety | safe API | C interop or a measured hot path: Unsafe* |
struct and enum. Use class for identity, shared mutable state, inheritance or resource lifetime. A window or a database connection has identity. A Point does not.let. Change to var only when you mutate.enum, not as several optional properties. Invalid combinations then cannot exist, and a state change is one assignment.precondition or fatalError for a programmer mistake.Read TYPES-AND-API.md for copy-on-write, noncopyable types, error design, protocols, some against any, API naming and macros.
Agents get this wrong most often, because the model changed in Swift 6.2: an async function runs on the actor of its caller. It does not leave the main thread.
Take these steps in order, and take the next one only when a profile gives the reason:
async/await to wait on the network or the disk. SDK calls such as URLSession.data(from:) move the work off the thread for you.@concurrent on your own expensive function, after Instruments shows a hang.actor to move state off the main actor, when main actor state forces tasks to hop back all the time.Set the build first. Enable Approachable Concurrency in every project. For app and UI modules, set Default Actor Isolation to MainActor (in a package: .defaultIsolation(MainActor.self)). Do not set it for a general library: a library ships nonisolated APIs so the caller decides where work runs.
| Annotation | Where the function runs |
|---|---|
@concurrent | Always on the concurrent pool. For your CPU-heavy work. |
nonisolated | Where the caller runs. The default for library APIs. |
| neither | On the actor of the caller |
nonisolated struct PhotoProcessor {
@concurrent
func process(_ data: Data) async -> ProcessedPhoto {
async let sticker = extractSticker(data) // two independent jobs in parallel
async let colors = extractColors(data)
return await ProcessedPhoto(sticker: sticker, colors: colors)
}
}
Rules that prevent most bugs:
await is a suspension point. State can change across it, so check your assumptions again after it. Do not hold a lock across an await.async let for a fixed count, withTaskGroup for a dynamic count. Use Task { } only for work that starts from a UI event or callback. Almost never use Task.detached.Sendable on purpose.When the compiler reports a data race, try these fixes in order:
Sendable value type, so sharing is copying.Mutex or Atomic from Synchronization, or @unchecked Sendable.Read CONCURRENCY.md before you write actors, task groups, cancellation, continuations or AsyncStream, when you fix a Sendable or isolation error, for concurrency in SwiftUI, and to migrate a codebase to Swift 6.
some P. Change to any P when you store mixed types.Do the algorithm work first, then profile with Instruments, then change code. Know the cost of each call: remove(at:) in a loop is O(n^2), and removeAll(where:) is O(n).
Use weak and unowned only to break a reference cycle. A better fix is to remove the cycle: move the shared data into a third type, so the graph is a tree.
Read PERFORMANCE.md when a profile shows a problem, when you tune a hot path, for ARC and object lifetime, and before you write unsafe code or C interop.
Write new tests with Swift Testing: @Test, #expect, try #require, and @Test(arguments:) in place of a loop. Log with Logger from os, not print.
Read TESTING-AND-TOOLS.md when you write or migrate tests, add logging, or debug tasks in LLDB.
Agents often write an older, longer form. Read MODERN-SYNTAX.md before you write a nested ternary, ObservableObject, NotificationCenter with userInfo, Process with pipes, string index arithmetic, or withUnsafeBufferPointer.
| Need | Use | Not |
|---|---|---|
| Shared mutable state | actor, or a @MainActor class | a class with a lock you must remember |
| Work off the main thread | @concurrent func ... async | Task.detached, DispatchQueue.global() |
| Isolation of a library API | nonisolated | @MainActor, @concurrent |
| Fixed number of parallel jobs | async let | several unstructured Tasks |
| Dynamic number of parallel jobs | bounded withTaskGroup | one task per element with no limit |
| Children that return nothing | withDiscardingTaskGroup | a withTaskGroup you never drain |
| Work tied to a UI event | Task { } inside the callback | an async callback |
| A shared model class | non-Sendable, or @MainActor | Sendable with manual locks |
A wait across await | restructure the code | DispatchSemaphore, NSCondition |
| Mixed collection | [any P] | a class hierarchy |
| Shared behavior with no customization | a constrained extension | a new protocol |
| Breaking a reference cycle | restructure to a tree | weak plus withExtendedLifetime |
| Contiguous memory access | .span | withUnsafeBufferPointer |
| Fixed-size buffer in a hot path | InlineArray<N, T> | Array |
| A test that fails for now | withKnownIssue | .disabled, or a commented-out test |
| Stopping a test on failure | try #require | continueAfterFailure = false |
| Diagnostics in shipped code | Logger with a correlation ID | print |
| A decision to optimize | Instruments on a profiled test | intuition |
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
swift-concurrency, data-race-safety, swift-6, arc, api-design
85 skills in this plugin
/alignmentMeasures a running page for edges, gaps, centres and radii that miss by a pixel or two, judges visual weight, and returns a numbered report with proof./animateBuilds a web animation from scratch and writes the code. Decides in order whether it should animate at all, its purpose, the tool, the properties, the easing and duration or spring, how it interrupts and exits, and reduced motion. Use when asked to animate something, add motion, build a transition, or make a component feel alive. For React Native or Expo use animate-expo. To critique existing motion use review-animations. To audit a whole codebase use improve-animations./animate-expoOther plugins' skills for the same job
npx claudepluginhub officialunofficial/skills --plugin officialunofficial-skills