swift-development
Scannednpx machina-cli add skill ProjAnvil/MindForge/swift-development --openclawSwift 6 Development Skill
Instructions
Follow this guide when working on non-UI Swift code, business logic, algorithms, or the data layer. Your goal is to write high-performance, thread-safe, and expressive Swift code.
What This Skill Does
- Concurrent Programming: Write safe concurrent code using Actors, async/await, Task, and TaskGroup.
- Type Safety: Define Sendable types and use Typed Throws for precise error handling.
- Testing: Write modern unit tests using the Swift Testing framework.
- Data Processing: Write Codable models and encapsulate JSON parsing and network requests.
- Macros: Use or create macros to reduce boilerplate.
When to Use This Skill
- When writing ViewModel, Service, or Repository layer code.
- When defining data models (structs, enums).
- When implementing algorithms or utility functions.
- When writing unit tests.
- When fixing concurrency warnings or data races.
Examples
Example 1: Defining a Safe Actor Service
/// A thread-safe user data service
actor UserService {
private var cache: [String: User] = [:]
// Use typed throws to specify the error type explicitly
func fetchUser(id: String) async throws(NetworkError) -> User {
if let cached = cache[id] {
return cached
}
// Simulate network request
let user = try await NetworkClient.shared.get("/users/\(id)", as: User.self)
cache[id] = user
return user
}
}
Example 2: Testing with Swift Testing
import Testing
@testable import MyApp
@Test("User parsing should succeed")
func userParsing() async throws {
let json = """
{ "id": "1", "name": "Alice" }
""".data(using: .utf8)!
let user = try JSONDecoder().decode(User.self, from: json)
#expect(user.name == "Alice")
#expect(user.id == "1")
}
Best Practices
- Conform to Sendable: Any type passed between concurrency domains must conform to the
Sendableprotocol. - Avoid Global Mutable State: Avoid global variables unless protected by an Actor.
- Implicitly Unwrapped Optionals: Strictly forbidden (the force unwrap operator), except where unavoidable in initializers (very rare).
- Use
letovervar: Default to constants; use variables only when necessary. - Error Handling: Prefer
throwsanddo-catchover returningOptionalorResulttypes (throws is more natural in async contexts).
Notes
- Always pay attention to "Strict Concurrency Checking" compiler warnings.
- When handling closures across Actor boundaries, be mindful of
[weak self]or[unowned self](though usually not needed inTaskunless breaking a reference cycle).
Source
git clone https://github.com/ProjAnvil/MindForge/blob/main/skills/en/swift-development/SKILL.mdView on GitHub Overview
Provides core Swift 6+ language development capabilities for non-UI code, including concurrency, macros, model design, and business logic. It emphasizes high-performance, thread-safe code, expressive data models, and robust testing across the data and logic layers.
How This Skill Works
Developers write non-UI Swift code in data and business logic layers, leveraging Actors, async/await, and TaskGroup for safe concurrency. They define Sendable types, use Typed Throws for precise errors, create Codable models for JSON parsing, and apply macros to reduce boilerplate. Tests are written with the Swift Testing framework to validate behavior.
When to Use It
- When implementing ViewModel, Service, or Repository layer code.
- When defining data models (structs, enums).
- When implementing algorithms or utility functions.
- When writing unit tests.
- When fixing concurrency warnings or data races.
Quick Start
- Step 1: Scaffold non-UI Swift modules (ViewModel/Service/Repository) and define core data models.
- Step 2: Introduce Actors, async/await, and Codable models; mark types as Sendable and use typed throws where appropriate.
- Step 3: Add unit tests with the Swift Testing framework and iterate to address any Strict Concurrency Warnings.
Best Practices
- Conform to Sendable: ensure cross-thread safety of types.
- Avoid global mutable state: protect with Actors.
- Implicitly Unwrapped Optionals: avoid except in rare initializers.
- Use let over var by default; prefer immutability.
- Prefer throws and do-catch over returning Optional or Result in async contexts.
Example Use Cases
- Example 1: Defining a Safe Actor Service - a thread-safe UserService actor with an internal cache and typed throws for fetchUser.
- Example 2: Testing with Swift Testing - unit tests validating data models and parsing using the Swift Testing framework.
- Example 3: Codable Data Models - designing Codable structs/enums to model JSON responses and network payloads.
- Example 4: Macro Utilization - applying macros to reduce boilerplate in repetitive model or boilerplate-heavy code paths.
- Example 5: Type-Safe Concurrency - using Sendable types and typed throws to enforce concurrency safety across modules.