跳至主要内容
小龙虾小龙虾AI
🤖

Swift

Write safe Swift code avoiding memory leaks, optional traps, and concurrency bugs.

下载2.2k
星标2
版本1.0.1
写作创作
安全通过

技能说明


name: Swift slug: swift version: 1.0.1 description: Write safe Swift code avoiding memory leaks, optional traps, and concurrency bugs. metadata: {"clawdbot":{"emoji":"🦅","requires":{"bins":["swift"]},"os":["darwin","linux"]}}

Quick Reference

TopicFile
Optionals, nil safety, force unwrapoptionals.md
Retain cycles, weak refs, closuresmemory.md
async/await, actors, Sendable, value typesconcurrency.md
JSON encoding/decoding trapscodable.md
Protocols, collections, strings, errors, buildtypes.md
SwiftUI state (@State, @Binding, Combine)swiftui.md
Property wrappers, actors, result builders, macrosadvanced.md
XCTest pitfalls, SPM gotchastesting.md

Critical Rules

Memory & Safety

  • Force unwrap ! crashes on nil — use guard let or if let instead
  • Closures capturing self strongly create retain cycles — use [weak self] in escaping closures
  • Delegates must be weak — strong delegate = object never deallocates
  • try! crashes on any error — never use in production paths
  • removeFirst() crashes on empty — use popFirst() for safety

Concurrency

  • async let starts immediately — not when you await
  • Actor reentrancy at every await — state may change between suspension points
  • @MainActor doesn't guarantee immediate main thread — it's queued
  • Sendable conformance violations crash at runtime — compiler warnings are errors

Types & Collections

  • Protocol extensions don't override — static dispatch ignores subclass implementation
  • Mutating struct in collection requires reassignment — array[0].mutate() doesn't work
  • String.Index from one string invalid on another — even if contents match

SwiftUI

  • @StateObject owns, @ObservedObject borrows — recreating view loses ObservedObject state
  • @EnvironmentObject crashes if not injected — no compile-time check
  • View identity change resets all @State — changing ID loses state

Build

  • print() builds strings even in release — remove or use os_log
  • Generic code bloat — specialized for each type, increases binary size

如何使用「Swift」?

  1. 打开小龙虾AI(Web 或 iOS App)
  2. 点击上方「立即使用」按钮,或在对话框中输入任务描述
  3. 小龙虾AI 会自动匹配并调用「Swift技能完成任务
  4. 结果即时呈现,支持继续对话优化

相关技能