AGENT SKILL
swift-language
Standards for Optionals, Protocols, Extensions, and Type Safety. Use when working with Swift Optionals, Protocols, Extensions, or type-safe APIs. (triggers: **/*.swift, protocol, extension, optional, guard, enum)
- License 许可
- MIT
- 最近更新
- 2026-06-24
- Stars
- ★ 67
- Forks
- 24
01 前言 Front Matter
- name
- swift-language
- description
- Standards for Optionals, Protocols, Extensions, and Type Safety. Use when working with Swift Optionals, Protocols, Extensions, or type-safe APIs. (triggers: **/*.swift, protocol, extension, optional, guard, enum)
02 资源文件 19
- 参考文档agents/analyzer.md10.1 KB
- 参考文档agents/comparator.md7.1 KB
- 参考文档agents/grader.md8.8 KB
- 资产文件assets/eval_review.html6.9 KB
- 脚本eval-viewer/generate_review.py16.0 KB
- 参考文档eval-viewer/viewer.html43.9 KB
- 许可证LICENSE.txt11.1 KB
- 参考文档references/output-patterns.md1.8 KB
- 参考文档references/schemas.md11.8 KB
- 参考文档references/workflows.md818 B
- 脚本scripts/aggregate_benchmark.py14.1 KB
- 脚本scripts/generate_report.py12.5 KB
- 脚本scripts/improve_description.py10.9 KB
- 脚本scripts/init_skill.py13.4 KB
- 脚本scripts/package_skill.py3.2 KB
- 脚本scripts/quick_validate.py3.2 KB
- 脚本scripts/run_eval.py11.2 KB
- 脚本scripts/run_loop.py13.3 KB
- 脚本scripts/utils.py1.6 KB
03 SKILL.md 正文
Swift Language Standards
Priority: P0
Implementation Guidelines
Optionals & Safety
- Never Force Unwrap: Use guard let, if let, or nil coalescing (??) to safely unwrap.
- Nil Comparison: Use
value != nilinstead ofif let _ = value. - Implicitly Unwrapped: Avoid
Type!. Use properType?.
Protocols & Extensions
- Protocols as Blueprints: Protocols define a blueprint; classes inherit implementation.
- Composition over Inheritance: Prefer protocol composition with structs for better decoupling and performance.
- Extensions: Implement conformance in an extension:
extension MyType: MyProtocol { ... }. Use extensions for clean code organization, not storage (do not use extensions for stored properties). - Protocol Witnesses: Explicitly implement all required members to satisfy the witness table.
Type Safety
- Avoid
Any: Use generics or associated types instead for compile-time safety. - Enums: Prefer enums with associated values over multiple Optionals. Extract values via a switch statement (e.g.,
case .success(let value):). - Value Types: Default to struct for value semantics and thread safety. Use
classonly when reference identity is necessary or for inheritance. Structs are copied; classes are shared.
Anti-Patterns
- No ! operator: Use safe unwrapping.
- No -1 for failure: Use Optional.
- No force cast (as!): Use conditional cast (as?).