write-swift 是什么?
How to write modern Swift well — modeling with value types, Swift 6 data-race safety and approachable concurrency (@concurrent, main-actor-by-default, actors, task groups), protocols and generics (some vs any), API design, performance and ARC, Swift Testing, macros, and the modern language features agents don't know about yet. Use when writing, reviewing, or migrating Swift, or when a concurrency error, a hang, a data race, a retain cycle, or a performance problem needs fixing.
以上为来源原文,中文用途说明尚待整理。
怎么接入你的 Agent?
- 定位完整 Skill 目录
打开本项目来源,核对
SKILL.md及其引用的脚本和资料。 - 交给你正在用的 Agent
在下面选择客户端,展开并复制安装指令,让 Agent 读取来源后选择安装方式。
- 确认识别,再做具体任务
让 Agent 回报安装目录和识别到的名称
write-swift,然后告诉 Agent 你的任务和实际输入,让它先核对触发条件。
展开并复制给 Codex 的安装指令
安装并检查 write-swift
请在 Codex 的当前工作环境中安装并检查 write-swift 这个 Skill。
项目身份:emilkowalski/skills / write-swift
来源:https://github.com/emilkowalski/skills/blob/64eb3deb9a5fbfa041faf783e0bc3fd635872e9c/skills/write-swift/SKILL.md
已定位的源文件:skills/write-swift/SKILL.md
1. 先阅读真实 SKILL.md 和它引用的必要文件,确认触发条件、安装方式、依赖与权限;尊重源文件要求的审批步骤。
2. 按当前客户端支持的方式安装完整 Skill 目录,保留已有配置,不只复制一段提示词。涉及缺失凭证、目标目录或权限时先列出缺项并向我确认。
3. 安装后回读实际目录、Skill 名称和识别结果,说明哪些步骤已完成,哪些只是文档检查,不得把安装成功当作任务已经实测。
4. 如来源提供了可安全验证的示例,在我提供所需输入后做一个最小任务;创建、修改、删除、部署或发送内容前,按源文件和我的授权执行。展开并复制给 Claude Code 的安装指令
安装并检查 write-swift
请在 Claude Code 的当前工作环境中安装并检查 write-swift 这个 Skill。
项目身份:emilkowalski/skills / write-swift
来源:https://github.com/emilkowalski/skills/blob/64eb3deb9a5fbfa041faf783e0bc3fd635872e9c/skills/write-swift/SKILL.md
已定位的源文件:skills/write-swift/SKILL.md
1. 先阅读真实 SKILL.md 和它引用的必要文件,确认触发条件、安装方式、依赖与权限;尊重源文件要求的审批步骤。
2. 按当前客户端支持的方式安装完整 Skill 目录,保留已有配置,不只复制一段提示词。涉及缺失凭证、目标目录或权限时先列出缺项并向我确认。
3. 安装后回读实际目录、Skill 名称和识别结果,说明哪些步骤已完成,哪些只是文档检查,不得把安装成功当作任务已经实测。
4. 如来源提供了可安全验证的示例,在我提供所需输入后做一个最小任务;创建、修改、删除、部署或发送内容前,按源文件和我的授权执行。展开并复制给 Cursor 的安装指令
安装并检查 write-swift
请在 Cursor Agent 的当前工作环境中安装并检查 write-swift 这个 Skill。
项目身份:emilkowalski/skills / write-swift
来源:https://github.com/emilkowalski/skills/blob/64eb3deb9a5fbfa041faf783e0bc3fd635872e9c/skills/write-swift/SKILL.md
已定位的源文件:skills/write-swift/SKILL.md
1. 先阅读真实 SKILL.md 和它引用的必要文件,确认触发条件、安装方式、依赖与权限;尊重源文件要求的审批步骤。
2. 按当前客户端支持的方式安装完整 Skill 目录,保留已有配置,不只复制一段提示词。涉及缺失凭证、目标目录或权限时先列出缺项并向我确认。
3. 安装后回读实际目录、Skill 名称和识别结果,说明哪些步骤已完成,哪些只是文档检查,不得把安装成功当作任务已经实测。
4. 如来源提供了可安全验证的示例,在我提供所需输入后做一个最小任务;创建、修改、删除、部署或发送内容前,按源文件和我的授权执行。展开并复制给 当前 Agent 的安装指令
安装并检查 write-swift
请在 当前 Agent 的当前工作环境中安装并检查 write-swift 这个 Skill。
项目身份:emilkowalski/skills / write-swift
来源:https://github.com/emilkowalski/skills/blob/64eb3deb9a5fbfa041faf783e0bc3fd635872e9c/skills/write-swift/SKILL.md
已定位的源文件:skills/write-swift/SKILL.md
1. 先阅读真实 SKILL.md 和它引用的必要文件,确认触发条件、安装方式、依赖与权限;尊重源文件要求的审批步骤。
2. 按当前客户端支持的方式安装完整 Skill 目录,保留已有配置,不只复制一段提示词。涉及缺失凭证、目标目录或权限时先列出缺项并向我确认。
3. 安装后回读实际目录、Skill 名称和识别结果,说明哪些步骤已完成,哪些只是文档检查,不得把安装成功当作任务已经实测。
4. 如来源提供了可安全验证的示例,在我提供所需输入后做一个最小任务;创建、修改、删除、部署或发送内容前,按源文件和我的授权执行。来源原文与资料状态
- 源文件
- 已保存 SKILL.md,抓取于 2026-11-19T19:32:10.677Z
- 中文使用说明
- 尚待整理,保留英文来源内容
- 安装与任务测试
- 本站未执行安装或任务测试
- 来源复核
- 已保留来源记录;当前完整用法未逐项人工复核
展开 SKILL.md 原文、章节与历史收录资料
skills/write-swift/SKILL.md · 抓取时间 2026-11-19T19:32:10.677Z
以下保留源文件的 description、章节及摘录,供核对中文说明;完整文件通过上方链接打开。
抓取成功不代表已完成安装或任务测试。
How to write modern Swift well — modeling with value types, Swift 6 data-race safety and approachable concurrency (@concurrent, main-actor-by-default, actors, task groups), protocols and generics (some vs any), API design, performance and ARC, Swift Testing, macros, and the modern language features agents don't know about yet. Use when writing, reviewing, or migrating Swift, or when a concurrency error, a hang, a data race, a retain cycle, or a performance problem needs fixing.
查看源文件章节 / Source sections
- 原文:Write Swift
- 原文:Initial Response
- 原文:1. Model data with value types
- 原文:2. Errors and optionals — make the failure paths visible
- 原文:3. Concurrency: stay single-threaded until profiling says otherwise
- 原文:The rule that changed
- 原文:Actor reentrancy
- 原文:4. Sendable and sharing data
- 原文:5. Structured concurrency
- 原文:6. Concurrency in SwiftUI
- 原文:7. Protocols and generics
- 原文:`some` vs `any`
- 原文:8. API design — clarity at the point of use
- 原文:9. Performance — measure, then choose
- 原文:10. ARC and object lifetime
- 原文:11. Testing — Swift Testing by default
- 原文:12. Macros
- 原文:13. Logging and debugging
- 原文:14. Unsafe code and interop
- 原文:15. Modern syntax you should be using
- 原文:16. Migrating an existing codebase to Swift 6
- 原文:Quick Reference
原文:Initial Response
When this skill is first invoked without a specific question, respond only with: > I'm ready to help you write modern Swift, the way the language wants to be written. Do not provide any other information until the user asks a question. How to write Swift the way the language wants to be written, current through Swift 6.4. **Toolchain baseline: Swift 6.3.** Check `swift --version`. Everything here compiles on 6.3 unless marked ⚠, which flags unreleased Swift 6.4 features. Concurrency guidance assumes the Swift 6.2 model — if the project is on 6.1 or earlier, §3's rules about `async` and `@concurrent` do not apply. The through-line: **Swift is a progressive-disclosure language. Start with the simplest, most static, most single-threaded thing that works, and buy dynamism — concurrency, reference semantics, existentials, unsafe pointers — only where you can point at the reason.** Every rule below is an application of that. Model this hierarchy of defaults. Move down a level only with a reason you can state: | Need | Reach for | Move down only when | | ------------ | ----------------------- | ---------------------------------------------------------- | | Data | `struct` / `enum` | you need identity, sharing, or inheritance | | Abstraction | concrete type | you have repeated code across types | | Polymorphism | `some P` (generic) | you need heterogeneous storage → `any P` | | Execution | main actor, synchronous | profiling shows a hang → `async` → `@concurrent` → `actor` | | Memory | `Array`, `String` | profiling shows the cost → `InlineArray`, `Span` | | Safety | safe API | C interop or a measured hot path → `Unsafe*` | ---
原文:1. Model data with value types
Value types are the default in Swift, not a special case. - **Default to `struct` and `enum`. Use `class` only for identity, shared mutable state, inheritance, or resource lifetime.** A window, a database connection, an entity stored in a rendering engine — those have identity. A `Point`, a `Drink`, a `Material` does not. - **`let` by default; `var` only when you mutate.** This is the same discipline as `some` before `any` and value before reference: start narrow, widen with cause. - **A struct with a mutable reference-type property is neither a value nor a reference.** Copies share the object; mutations leak across copies. Either keep the referenced type immutable, or make it a `private` stored property behind copy-on-write. Computed properties that forward to it don't help on their own: a forwarding setter still mutates the shared object. - **Copy-on-write is how you get out-of-line storage _and_ value semantics.** Wrap a final class in a struct and check `isKnownUniquelyReferenced(&storage)` before mutating; copy first if it isn't. This is exactly how `Array`, `String`, and `Dictionary` work. - **Enums are the tool for "a fixed set of things" and for mutually exclusive state.** Replacing a pile of optional stored properties (`isSharing`, `selectedRows`, `shareTarget`) with one `enum State` makes invalid combinations unrepresentable and makes state change atomic instead of a sequence of property writes you can forget to finish. - **Composing values yields a value.** A struct whose stored properties are all value types has value semantics for free — which is what makes undo, diffing, and state restoration a single code path instead of one per property. ```swift struct Material { // value semantics preserved var roughness: Double private var _texture: Texture // a class var color: Color { get { _texture.color } set { if !isKnownUniquelyReferenced(&_texture) { _texture = Texture(copying: _texture) } _texture.color = newValue } } } ```
本段为节选,完整内容见源文件。
原文:2. Errors and optionals — make the failure paths visible
Swift error handling rests on three points: sources of error are marked so they can't surprise you; errors carry enough context to act on; and **recoverable errors are different from programmer mistakes**. - **Recoverable → `throw`. Programmer mistake → `precondition`/`fatalError`.** A failed network call keeps the program running. An out-of-bounds index means the code is wrong and must halt before the bug becomes a security issue. - **Enums with associated values make the best error types.** `case duplicateFriend(String)` beats `case duplicateFriend` — the context is the whole point. - **`guard` for error conditions**, because it forces the exit path. `if let` for the ordinary unwrap. - **Typed throws (`throws(MyError)`) are for internal functions, error-forwarding generic code, and constrained environments** where boxing `any Error` is too costly. For public API, untyped `throws` preserves your freedom to change the error type later. Note the unification: `throws` is `throws(any Error)`, and non-throwing is `throws(Never)` — which is what lets `map` abstract over both. - **Force-unwrap only where you can state the invariant**, and prefer a failing `#require`/`precondition` with a message over a bare `!`. ---
原文:3. Concurrency: stay single-threaded until profiling says otherwise
This is the section agents get wrong most often, because the model changed in Swift 6.2. Start every app entirely on the main thread. Single-threaded code goes a long way, and most apps never need to leave it. **The progression, in order. Do not skip steps.** 1. **Single-threaded on the main actor.** No concurrency at all. Fine for most apps. 2. **`async`/`await`** to hide latency (network, disk). Still no concurrency of your own — SDK APIs like `URLSession.data(from:)` offload on your behalf. 3. **`@concurrent`** to move _your_ expensive work off the main thread — only after Instruments shows a hang. 4. **`actor`** to move _state_ off the main actor — only when too much main-actor state is forcing tasks to hop back constantly. **Turn on the right build settings first.** Enable **Approachable Concurrency** in every project. For app modules and UI-facing modules, also set **Default Actor Isolation** to **MainActor** — it's the default for new app projects in Xcode 26, and it deletes most of your `@MainActor` annotations. In a package: `swiftSettings: [.defaultIsolation(MainActor.self)]`. **Do not set main-actor-by-default for a general-purpose library** — libraries should ship `nonisolated` APIs and let clients decide where work runs.
原文:The rule that changed
**With Approachable Concurrency on (Swift 6.2+, which enables `NonisolatedNonsendingByDefault`), marking a `nonisolated` function `async` does _not_ move it off the caller's actor.** It runs where it was called from. This is what makes "the most natural code to write" data-race free by default. - **`@concurrent`** — always switches to the concurrent thread pool. Use it on _your_ CPU-heavy work. - **`nonisolated`** — runs wherever it's called from. **This is the right default for library APIs**, because the caller decides. `nonisolated` on a type makes all its members nonisolated (Swift 6.1+). - Neither one — stays on the caller's actor. ```swift nonisolated struct PhotoProcessor { // decoupled from the main actor @concurrent // guaranteed to run in the background 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) } } ``` - **Profile before you offload.** Use Instruments (Time Profiler, hangs). If the code can be made faster without concurrency, always do that first. Concurrency has real cost — task allocation, scheduling, and reasoning. - **Don't spawn a task for trivial work.** A child task to read a `UserDefaults` value costs more than it saves. - **One task per end-to-end operation.** Work that must happen in order goes in _one_ task; independent operations get separate tasks so the runtime can interleave them. - **`await` is a suspension point, and it breaks atomicity.** State can change while you're suspended, and you may resume on a different thread. Re-check assumptions after every `await`. Never hold a lock across one. Never rely on thread-local storage across one.
原文:Actor reentrancy
Actors guarantee mutual exclusion, not transactions. Between two `await`s on the same actor, other work runs. - **Mutate actor state in synchronous methods.** Synchronous code on an actor runs to completion uninterrupted — that's your transaction boundary. - **Keep async actor methods thin**, composed of synchronous transactional operations, and leave the actor in a consistent state at every `await`. - The classic bug: check cache → `await` download → write cache. Two tasks both miss, both download, the second clobbers the first. Re-check after the `await`, or dedupe the in-flight work. - **Actors are not FIFO.** They run highest-priority work first, precisely to avoid priority inversion. If you need ordering, use a task (which runs start to finish) or an `AsyncStream`, not an actor. ---
文件 SHA256:ad8f3af70b627c165ecd73ecdba49449e35301dd845c397cf813f06582bbdac2
仓库许可证记录:MIT;repository-license-recorded-scope-unverified,查看来源。
How to write modern Swift well — modeling with value types, Swift 6 data-race safety and approachable concurrency (@concurrent, main-actor-by-default, actors, task groups), protocols and generics (some vs any), API design, performance and ARC, Swift Testing, macros, and the modern language features agents don't know about yet. Use when writing, reviewing, or migrating Swift, or when a concurrency error, a hang, a data race, a retain cycle, or a performance problem needs fixing.
历史收录数据
| 来源仓库 | emilkowalski/skills |
|---|---|
| Skill源路径 | skills/write-swift/SKILL.md |
安装前请让使用的Agent先读取这个SKILL.md及其所在目录,确认依赖和所需权限。此页不生成未经来源验证的安装命令。
