ams-swift-pro
暂无简介
- License 许可
- MIT
- 最近更新
- 2026-11-07
- Stars
- ★ 0
- Forks
- 1
03 SKILL.md 正文
Swift Pro
Overview
Swift rewards safety by construction: optionals instead of nulls, value types by default, protocols over inheritance, and a compiler that catches entire bug families. Professional Swift means leaning into these — modeling with structs and enums, handling optionals explicitly, using structured concurrency instead of callback pyramids, and writing code the compiler can verify.
The through-line: make invalid states unrepresentable, handle the absent case explicitly, and let the type system do the worrying.
When to use
- Writing or reviewing Swift code for idiom and safety.
- Structuring iOS/macOS apps (SwiftUI or UIKit).
- Choosing value vs reference types, or designing protocol abstractions.
- Using async/await, actors, and structured concurrency.
- Debugging optionals, retain cycles, or concurrency issues.
Core concepts
- Value types by default.
structfor data and domain models — copied on assignment, no shared-mutable-state surprises, thread-safe by construction.classonly when you need identity, shared mutable state, or inheritance (rare). Enums with associated values model states beautifully. - Optionals are explicit.
if let/guard letfor unwrapping,map/flatMapfor transforms, nil-coalescing for defaults. Force-unwrap (!) only for true invariants (IBOutlets after load) — never on network data or user input. Implicitly-unwrapped optionals are a code smell outside narrow cases. - Protocols + extensions. Protocol-oriented design: small protocols, default implementations via extensions, composition over inheritance. But don't protocol-everything preemptively — concrete types first, protocols when you have two conformers or a testing seam.
- Structured concurrency.
async/awaitfor asynchronous work,Taskgroups for fan-out,actorfor isolated mutable state. Say goodbye to callback pyramids and manual GCD queue hopping. Respect@MainActorfor UI updates; don't block the main actor. - Error handling with
throws. Typed throws (Swift 6) for precise error modeling;do/catchat boundaries,try?/try!only where failure is truly uninteresting/impossible.Resulttype when errors are values to pass around rather than control flow. - Memory: ARC + weak/strong discipline. Retain cycles via closures capturing
selfstrongly —[weak self]in escaping closures that outlive their context. Value types sidestep most of this; reference cycles are a class-design problem.
Practical workflow
- Model first. Structs for data, enums with associated values for states/results, protocols for the seams you'll actually test or vary.
- Handle absence explicitly. No force-unwraps on external data;
guard letearly for preconditions; meaningful defaults via??. - Write async code structurally.
asyncfunctions,TaskGroupfor parallel work, actors for shared mutable state. Migrate legacy completion handlers withwithCheckedContinuationat the boundary. - Organize by feature. Group related models, views, and logic (
Features/Checkout/) rather than by type (Models/,Views/). Keep views dumb; move logic to view models or domain types. - Test the logic, not the framework. Unit-test domain types, view models, and parsers with XCTest/Swift Testing; UI tests only for critical journeys. Inject dependencies via protocols for testability.
- Mind the platform. Respect main-actor isolation, handle background/foreground transitions, test on device for performance and memory, and profile with Instruments before optimizing.
Idiomatic snippets:
// Enum states + async/await + guard
enum LoadState<T> {
case idle, loading, loaded(T), failed(Error)
}
func refresh() async {
state = .loading
do {
let items = try await store.fetchItems()
state = .loaded(items)
} catch {
state = .failed(error)
}
}
// Weak capture in escaping closures
service.onUpdate = { [weak self] items in
guard let self else { return }
self.items = items
}
Common pitfalls
- Force-unwrap roulette.
!on optionals from JSON, user input, or casts — crashes in production. Every!should have a justification a reviewer can verify. - Massive view controllers / views. 1,000-line view files doing networking, parsing, and layout. Extract: view models for state, services for I/O, small composable views.
- Retain cycles. Closures capturing
selfstrongly in long-lived objects (timers, network callbacks, notification observers).[weak self]+guard let selfis the reflex. - Blocking the main thread. Synchronous networking, JSON parsing of large payloads, or image processing on main — jank and watchdog kills. Move to background, update UI on main actor.
- Over-protocoling. A protocol for every struct "for testability" before there's a second implementation. Concrete types are simpler; add protocols at real seams.
- Ignoring Swift 6 concurrency checking. Data races flagged by the compiler are real bugs.
Adopt
Sendable, respect actor isolation — don't silence with@unchecked Sendablewithout understanding why. - Stringly-typed APIs. String constants for states, notification names as raw strings.
Enums, typed notification wrappers, and
#function-based identifiers prevent typo-bugs.
04 同仓库更多 Skills
web-scrape
Turn web pages into LLM-ready markdown via the Firecrawl API (renders JS, anti-bot handling). Use when browser_open/fetch fails on JS-heavy pages, or when you need clean markdown/structured extraction from a site. Requires FIRECRAWL_API_KEY.
design-taste-frontend
tasteskill v2, the anti-slop frontend skill. Art direction for websites, distilled from a study of 247 curated gallery sites (One Page Love, Awwwards Sites of the Day). Deals researched directions (idea, composition, type, palette, photographs, section blueprints) from a library, enforces a strict no-slop house style with a real-browser inspector, and runs a look-and-fix loop on screenshots. Use this whenever you build, design, redesign, restyle, polish or review a landing page, marketing site, portfolio, product page, one-pager, hero or any public-facing web page, in any stack (HTML/CSS, React, Next.js, Vue, Svelte, Astro, Tailwind) — also when the user says a page looks generic, templated, "AI-made", sloppy, or asks to make it look designed, premium, award-level or less like every other site. Not for dashboards or app screens.
test-driven-development
Use when implementing any feature or bugfix, before writing implementation code
systematic-debugging
Use when encountering any bug, test failure, or unexpected behavior, before proposing fixes
requesting-code-review
Use when completing tasks, implementing major features, or before merging to verify work meets requirements
brainstorming
You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).