skillfed

Swift Idioms

This skill covers the core patterns that make Swift code safe and maintainable. You'll learn when to use structs over classes, how to handle optionals without crashes, design with protocols instead of inheritance, and leverage Swift's concurrency model. The guide includes practical examples of error handling, property wrappers, actors, and testing strategies—plus anti-patterns to avoid.

Swift Idioms teaches value types, optionals, protocol-oriented design, and concurrency patterns for safe, expressive Swift code.

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

150 48 MIT updated by irahardianto

Install

irahardianto/awesome-agv/swift-idioms · repository language: JavaScript

CLI (skillfed)coming soon
git clone https://github.com/irahardianto/awesome-agv
cp -r awesome-agv/.agents/skills/swift-idioms ~/.claude/skills/swift-idioms

Frequently asked questions

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

How do I write idiomatic Swift code that follows best practices?

Swift Idioms teaches you to prioritize value types (structs and enums) over classes, use guard let and optional binding instead of force unwrapping, and adopt protocol-oriented design to avoid deep inheritance hierarchies. The skill emphasizes naming conventions (camelCase for variables and methods), proper error handling with typed throws, and leveraging Swift's type system for safety. You'll learn patterns like defer for cleanup, Result types for async callbacks, and property wrappers for reducing boilerplate—all with practical examples that show why each pattern matters.

What's the difference between Swift value types and classes?

Swift Idioms explains that structs and enums are value types—they're copied when assigned or passed, making them safer for concurrent code and easier to reason about. Classes are reference types that share state, which can lead to unexpected mutations and threading issues. The skill shows when to choose each: use structs for data models and simple logic, classes only when you need reference semantics or inheritance. Understanding this distinction is foundational to writing safe, maintainable Swift.

How should I handle Swift optionals safely without force unwrapping?

Swift Idioms covers safe optional handling patterns: guard let for early returns, if let for conditional binding, and the nil-coalescing operator (??) for defaults. Force unwrapping (!) crashes your app if the value is nil, so the skill teaches you to avoid it except in rare cases where you're absolutely certain a value exists. You'll learn when to use optional chaining, how to chain multiple optionals cleanly, and how to design APIs that minimize optional propagation—keeping your code both safe and readable.

What is protocol-oriented design and how does it differ from inheritance?

Swift Idioms teaches protocol-oriented design as a way to define behavior through composition rather than deep class hierarchies. Protocols let you specify what a type must do without forcing a specific implementation path, and types can conform to multiple protocols. This approach is more flexible than inheritance, avoids the fragile base class problem, and works naturally with Swift's value types. The skill includes practical examples of designing with protocols, using protocol extensions for default behavior, and combining protocols with generics for powerful, reusable code.

How do I use async/await and structured concurrency correctly in Swift?

Swift Idioms covers async/await as the modern way to write concurrent code without callback pyramids. You'll learn to mark functions async, use await to suspend execution, and structure work with Task and TaskGroup for dynamic concurrency. The skill explains actors for thread-safe state, Sendable for type-safe data sharing across isolation boundaries, and how to avoid common pitfalls like detached tasks that outlive their scope. Proper async/await usage prevents race conditions and makes concurrent logic much clearer.

What anti-patterns and pitfalls should I avoid when coding in Swift?

Swift Idioms identifies key anti-patterns: force unwrapping optionals, using classes when structs fit better, ignoring compiler warnings, deep inheritance chains, and unsafe concurrency with shared mutable state. The skill warns against force casts, improper error handling, and detached tasks without proper cancellation. You'll learn why swiftlint and swift-format tools help catch these issues early, and how testing with XCTest and mock protocols catches logic errors before they reach production. Recognizing these pitfalls keeps your codebase maintainable and your apps stable.

SKILL.md

rendered from the published skill — quoted content, verbatim

Swift Idioms and Patterns

Swift rewards value types, optionals, and protocol-oriented design. Idiomatic Swift = safe, expressive, Swifty.

> Scope: Swift coding idioms. Test naming: .agents/rules/testing-strategy.md.

Value Types and Optionals

  1. Prefer structs over classes — value semantics by default. Classes only for identity, inheritance, or reference counting.
  2. Optionals — never force-unwrap (!) in production: ```swift // ✅ Guard let for early exit guard let task = storage.findById(id) else { throw TaskError.notFound(id) }

// ✅ Optional chaining let title = task?.title ?? "Untitled"

// ✅ if let for conditional binding if let deadline = task.deadline { scheduleReminder(for: deadline) }

// ❌ Force unwrap — crash risk let task = storage.findById(id)! ```

  1. Property wrappers for reusable behavior: ```swift @propertyWrapper struct Clamped<Value: Comparable> { var wrappedValue: Value { didSet { wrappedValue = min(max(wrappedValue, range.lowerBound), range.upperBound) } } let range: ClosedRange<Value>

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

Read as markdown · JSON record · Browse the source repository

File tree — 1 file
.agents/skills/swift-idioms/SKILL.md

Related skills

Tags

value-semantics optional-safety protocol-composition structured-concurrency type-safety memory-management error-propagation testability-patterns code-quality swift-6-features