swift-rules

Swift 编码规范,涵盖风格、设计模式、安全性和测试实践。用于在检测到相关 Swift 项目文件或技术时应用规范。触发条件:.swift、Package.swift、.xcodeproj、SwiftUI、Combine、async/await、XCTest。

仓库最近推送
GitHub Stars
180
Forks
21
Issues / PRs
0

安装方式

先用 Prompt 核对来源与依赖。也可下载安装包。

用 AI 助手安装

复制提示词,让助手先核对固定版本和依赖。

查看 Prompt收起 Prompt
先检查当前 Agent 是否已安装 SkillPI Skill Finder。如果尚未安装,请按官方仓库的说明安装并启用:https://github.com/Findskills/skillpi。然后使用 Skill Finder 打开此 SkillPI 详情页并定位 "swift-rules":https://skillpi.com/zh/skills/deepskill-4a36993af32c3f6cb7c602036ba268f30ecd3e98bfb548e936803728e7c241d7。安装前核对上游来源和版本:https://github.com/softspark/ai-toolkit。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。

来源与配置

github.com

配置方式待核实

请查看原始来源中的适用工具、依赖和权限要求。

安装实测待补充

Swift 编码规范,涵盖风格、设计模式、安全性和测试实践。用于在检测到相关 Swift 项目文件或技术时应用规范。触发条件:.swift、Package.swift、.xcodeproj、SwiftUI、Combine、async/await、XCTest。

0 下载

介绍来源: third_party · softspark · 0.0.1 参考来源

SkillPi 解读

能完成的任务
翻译待完成,请参阅来源文档。
输入和输出
翻译待完成,请参阅来源文档。
实测状态
未测试

文件浏览

在原站浏览文件 ↗

原始 SKILL.md

打开原文 ↗
Frontmatter
name: swift-rules
description: "Swift coding rules: style, patterns, security, testing. Triggers: .swift, Package.swift, .xcodeproj, SwiftUI, Combine, async/await, XCTest."
effort: medium
user-invocable: false
allowed-tools: Read

Swift Rules

These rules come from app/rules/swift/ in ai-toolkit. They cover the project's standards for coding style, frameworks, patterns, security, and testing in Swift. Apply them when writing or reviewing Swift code.

Swift Coding Style

Naming

  • PascalCase: types, protocols, enums, struct, class.
  • camelCase: functions, methods, properties, variables, enum cases.
  • No prefixes: Swift has module namespacing (no NS or UI prefix for your types).
  • Use descriptive names: removeElement(at:) not remove(i:).
  • Boolean properties read as assertions: isEmpty, hasChildren, canSubmit.

Types

  • Prefer struct over class by default (value semantics, no reference cycles).
  • Use class only when reference semantics or inheritance is required.
  • Use enum with associated values for modeling finite states.
  • Use protocol for defining capabilities. Prefer protocol composition.
  • Use typealias for complex generic signatures for readability.

Optionals

  • Use guard let for early exit on nil. Use if let for conditional binding.
  • Never force-unwrap (!) unless failure is a programming error.
  • Use ?? for default values: let name = user?.name ?? "Unknown".
  • Use optional chaining: user?.address?.city.
  • Use map / flatMap on optionals for transformations.

Properties

  • Use let by default. Use var only when mutation is required.
  • Use computed properties for derived values: var fullName: String { ... }.
  • Use property observers (willSet, didSet) for side effects on change.
  • Use lazy var for expensive initialization deferred until first access.
  • Use @Published (Combine) for observable properties in classes.

Functions

  • Use argument labels for clarity: func move(from source: Int, to destination: Int).
  • Omit argument labels when the function name makes the role clear: func contains(_ element: T).
  • Use default parameter values instead of multiple overloads.
  • Use throws / async throws for fallible operations.
  • Use trailing closure syntax for the last closure parameter.

Access Control

  • Use private for implementation details. Use fileprivate sparingly.
  • Use internal (default) for module-scoped access.
  • Use public for framework API. Use open only when subclassing is intended.
  • Prefer private(set) for read-only external access with internal mutation.

Formatting

  • Use SwiftLint for automated style enforcement.
  • Use SwiftFormat for automated code formatting.
  • Commit .swiftlint.yml and .swiftformat to the repository.
  • Max line length: 120 characters (SwiftLint default).
  • Use trailing commas in multi-line arrays and dictionaries.

Swift Frameworks

SwiftUI

  • Use VStack, HStack, ZStack for layout composition.
  • Use List with ForEach for dynamic content. Use LazyVStack for large lists.
  • Use NavigationStack (iOS 16+) with navigationDestination(for:) for type-safe navigation.
  • Use .task { } modifier for async data loading tied to view lifecycle.
  • Use @ViewBuilder for conditional view composition in custom containers.
  • Use PreviewProvider or #Preview macro for rapid UI iteration.

UIKit (Legacy / Hybrid)

  • Use UIHostingController to embed SwiftUI views in UIKit.
  • Use UIViewRepresentable to wrap UIKit views in SwiftUI.
  • Use Auto Layout with constraints or UIStackView for layout.
  • Use UICollectionViewCompositionalLayout for complex collection layouts.
  • Use Coordinator pattern for delegate-based UIKit interop in SwiftUI.

Combine

  • Use Publisher / Subscriber for reactive data streams.
  • Use sink for subscribing. Store cancellables in Set<AnyCancellable>.
  • Use map, filter, flatMap, combineLatest for stream transformation.
  • Use @Published on class properties for automatic publisher generation.
  • Prefer AsyncSequence (async/await) over Combine for new code.

Swift Data

  • Use @Model macro for persistent model definitions.
  • Use @Query in SwiftUI views for automatic fetching and observation.
  • Use ModelContext for CRUD operations: context.insert(item), context.delete(item).
  • Use #Predicate macro for type-safe query filtering.
  • Use ModelConfiguration for custom store locations and migration options.

Core Data (Legacy)

  • Use NSPersistentContainer for stack setup.
  • Use NSFetchRequest with NSPredicate for querying.
  • Use performBackgroundTask for background context operations.
  • Use lightweight migrations for schema changes when possible.
  • Prefer SwiftData for new projects (iOS 17+).

Vapor (Server-Side)

  • Use routes.get("users") for route definitions.
  • Use Content protocol for request/response body codable conformance.
  • Use Fluent ORM with migrations for database access.
  • Use middleware for authentication, CORS, and error handling.
  • Use async/await natively (Vapor 4+ is fully async).

Networking

  • Use URLSession with async/await for HTTP requests.
  • Use Codable with JSONDecoder for response parsing.
  • Use URLCache and ETag for response caching.
  • Set timeoutIntervalForRequest on URLSessionConfiguration.
  • Use TaskLocal for request-scoped values (tracing, auth context).

Package Management

  • Use Swift Package Manager (SPM) for dependency management.
  • Define dependencies in Package.swift with exact version or version ranges.
  • Use Package.resolved committed to the repository for reproducible builds.
  • Prefer SPM over CocoaPods/Carthage for new projects.

Swift Patterns

Error Handling

  • Use enum AppError: Error for typed, exhaustive error handling.
  • Use throws functions with do-catch for recoverable errors.
  • Use Result<Success, Failure> for asynchronous error propagation.
  • Use try? for optional conversion. Use try! only in tests or guaranteed paths.
  • Add LocalizedError conformance for user-facing error messages.

Protocol-Oriented Design

  • Define capabilities as protocols: protocol Fetchable { func fetch() async throws -> Data }.
  • Use protocol extensions for default implementations.
  • Use protocol composition: func process(_ item: Sendable & Codable).
  • Use associated types for generic protocols: associatedtype Output.
  • Use some Protocol (opaque types) for return types hiding concrete implementations.

Async/Await

  • Use async functions for all asynchronous operations.
  • Use async let for concurrent, independent operations.
  • Use TaskGroup for dynamic parallelism with collected results.
  • Use Task { } to bridge sync to async. Avoid .task { } in views for complex logic.
  • Use withThrowingTaskGroup for concurrent operations that can fail.

Actors

  • Use actor for thread-safe mutable state (replaces manual locks).
  • Use @MainActor for UI-related state and methods.
  • Use nonisolated for actor methods that do not access mutable state.
  • Use GlobalActor for custom isolation domains.
  • Minimize await calls on actors to reduce suspension points.

SwiftUI Patterns

  • Use @State for view-local mutable state.
  • Use @Binding for child-to-parent state communication.
  • Use @Observable (Observation framework) for model objects (preferred over @ObservedObject).
  • Use @Environment for dependency injection: @Environment(\.modelContext).
  • Use ViewModifier for reusable view transformations.
  • Extract subviews into separate structs for readability and performance.

Codable

  • Use Codable for JSON serialization/deserialization.
  • Use CodingKeys enum for custom key mapping.
  • Use JSONDecoder with .convertFromSnakeCase for API compatibility.
  • Use @propertyWrapper for custom decoding strategies (e.g., date formats).
  • Use nestedContainer for flattening nested JSON structures.

Dependency Injection

  • Use initializer injection for required dependencies.
  • Use @Environment in SwiftUI for framework-provided values.
  • Use swift-dependencies library for testable, controlled dependency management.
  • Use @Dependency(\.apiClient) var apiClient for automatic resolution.

Anti-Patterns

  • Force-unwrapping optionals: use guard let or ??.
  • Massive view controllers/views: split into subviews and view models.
  • Reference cycles: use [weak self] in closures capturing self.
  • Blocking the main thread: use Task or DispatchQueue.global().
  • Stringly-typed APIs: use enums, protocols, and strong types.

Swift Security

Keychain

  • Use Keychain Services for storing passwords, tokens, and cryptographic keys.
  • Use kSecAttrAccessibleWhenUnlockedThisDeviceOnly for sensitive items.
  • Use KeychainAccess or similar wrapper libraries for cleaner API.
  • Never store secrets in UserDefaults (unencrypted plist on disk).
  • Delete keychain items on user logout.

App Transport Security (ATS)

  • Use HTTPS for all network connections. ATS enforces this by default.
  • Never add blanket NSAllowsArbitraryLoads exception.
  • Use per-domain exceptions only when connecting to legacy servers.
  • Implement certificate pinning for high-security connections.
  • Validate server certificates in URLSessionDelegate for custom pinning.

Input Validation

  • Validate all user input before processing or displaying.
  • Use NSRegularExpression or Swift Regex for pattern validation.
  • Sanitize strings before using in URL construction, SQL, or HTML.
  • Validate deep link URL parameters before navigation.
  • Limit input lengths in UITextField / TextField to prevent abuse.

Data Protection

  • Use Data Protection API: set FileProtectionType.complete on sensitive files.
  • Use CryptoKit for hashing (SHA256), encryption (AES.GCM), and signing.
  • Use SecureEnclave for hardware-backed key storage on supported devices.
  • Zero sensitive data in memory after use: withUnsafeMutableBytes { $0.initializeMemory(as: UInt8.self, repeating: 0) }.
  • Use @Sendable closures to prevent data races in concurrent access.

Authentication

  • Use AuthenticationServices for Sign in with Apple and passkeys.
  • Use LocalAuthentication (Face ID / Touch ID) for biometric auth.
  • Store authentication tokens in Keychain, not in memory or UserDefaults.
  • Use short-lived access tokens with refresh token rotation.
  • Implement session timeout for inactive users.

Network Security

  • Use URLSession with certificate pinning for sensitive API calls.
  • Validate response Content-Type headers before parsing.
  • Use Codable for structured deserialization (prevents injection).
  • Set request timeouts to prevent hanging connections.
  • Do not log request/response bodies containing sensitive data.

Code Security

  • Use [weak self] in closures to prevent retain cycles and memory leaks.
  • Use @Sendable and actor isolation for thread-safe concurrent code.
  • Avoid UnsafePointer / UnsafeMutablePointer unless absolutely necessary.
  • Use #if DEBUG guards for debug-only code. Never ship debug features.
  • Enable Xcode hardened runtime for macOS apps.

Dependency Security

  • Audit SPM dependencies before adding. Check maintainer reputation.
  • Pin dependency versions in Package.resolved.
  • Review Package.swift of dependencies for unusual build plugins.
  • Prefer dependencies with active security response and disclosure processes.
  • Minimize third-party dependencies for security-critical modules.

Swift Testing

Framework

  • Use Swift Testing (import Testing) for new projects (Swift 5.10+).
  • Use XCTest for existing projects and UIKit-based UI tests.
  • Use swift-snapshot-testing for visual regression testing.
  • Use swift-dependencies for controlled dependency injection in tests.

File Naming

  • Test files: FooTests.swift in Tests/ target.
  • Mirror source module structure in test target.
  • Use @Test attribute (Swift Testing) or test prefix (XCTest) for test methods.
  • Use @Suite (Swift Testing) for test grouping.

Structure (Swift Testing)

  • Use @Test("description") for individual test cases.
  • Use @Test(arguments: [...]) for parameterized tests.
  • Use #expect(condition) for assertions. Use #require(condition) for preconditions.
  • Use #expect(throws: FooError.self) { try riskyOperation() } for error testing.
  • Use @Suite structs for grouping. Properties serve as shared setup.

Structure (XCTest)

  • Use setUp() / tearDown() for per-test initialization and cleanup.
  • Use setUpWithError() for throwing setup code.
  • Use XCTAssertEqual, XCTAssertTrue, XCTAssertNil for assertions.
  • Use XCTAssertThrowsError for exception testing.
  • Use expectation(description:) + wait(for:timeout:) for async assertions.

Async Testing

  • Use async test functions: @Test func fetchUser() async throws { ... }.
  • Use confirmation() (Swift Testing) for event-based async assertions.
  • XCTest: use XCTestExpectation with fulfillment() for callback-based async.
  • Test AsyncSequence with for await loops and assertion on collected values.

Mocking

  • Use protocol-based dependency injection for testability.
  • Create manual mock implementations conforming to protocols.
  • Use swift-dependencies for environment-controlled dependency overrides.
  • Use @Dependency property wrapper for automatic mock injection in tests.
  • Avoid mocking frameworks when protocol mocks are straightforward.

UI Testing (XCTest)

  • Use XCUIApplication for UI automation tests.
  • Use accessibility identifiers for reliable element lookup.
  • Use app.buttons["Submit"].tap() for interaction simulation.
  • Use waitForExistence(timeout:) for async UI element appearance.
  • Keep UI tests focused on critical user flows only (slow to run).

Best Practices

  • Test behavior through public API. Avoid @testable import when possible.
  • Use @testable import Module only when testing internal members is necessary.
  • Use withDependencies { } for scoped dependency overrides in tests.
  • Test on multiple platforms (iOS, macOS) when shipping cross-platform.
  • Run tests with swift test or xcodebuild test in CI.

版本历史

  1. v6.2.0 GitHub ↗
    原版更新说明

    **Full Changelog**: https://github.com/softspark/ai-toolkit/compare/v6.1.0...v6.2.0

  2. v6.1.0 GitHub ↗
    原版更新说明

    **Full Changelog**: https://github.com/softspark/ai-toolkit/compare/v6.0.0...v6.1.0

  3. v6.0.0 GitHub ↗
    原版更新说明

    ## What's Changed * chore: bump softprops/action-gh-release from 3.0.3 to 3.0.4 by @dependabot[bot] in https://github.com/softspark/ai-toolkit/pull/45 * fix: handle Windows status output and clarify Antigravity workflow support by @softspark in https://github.com/softspark/ai-toolkit/pull/46 **Full Changelog**: https://github.com/softspark/ai-toolkit/compare/v5.5.0...v6.0.0

  4. v5.5.0 GitHub ↗
    原版更新说明

    ### Ecosystem - **OpenCode V2 and V1 compatibility:** `scripts/generate_opencode_plugin.py` emits the documented `id/setup/server` contract for V2 and V1 **1.18.29+**. Upgrade older V1 clients before using this plugin, then run `ai-toolkit update` to regenerate managed copies. `doctor` warns about obsolete V1-only exports. Scoped global/project IDs avoid native collisions; advisory work has one owner per location, while safety guards execute in every registered layer. The current V2 package is `@opencode/cli`; `opencode-ai` still publishes V1. - **Claude guard failure policy:** destructive-command, home-path and configuration guards use `onFailure: block` on Claude Code **2.1.295+**. Legacy entries migrate without duplication; hooks owned by other plugins survive. Older clients do not have this infrastructure-failure guarantee. - **Codex, Gemini and Devin:** Codex refresh accepts the two diagnostic model-request events. Gemini advisory hooks return native JSON and save native completion responses; Devin prompt/edit hooks emit event-labelled context. - **All 13 integrations reviewed:** refresh release metadata and the ecosystem snapshot, distinguish stable/preview channels, and pin Auggie's stable v0 references. User model, effort and approval selections remain unchanged. ### Fixed - **One Claude Stop checker:** remove the duplicate advisory handler from the global and app manifests. `quality-gate.sh` keeps PHPStan advisory through `quality-check.sh --php-only`, preserving checker precedence and Makefile settings. PHPStan now runs synchronously on Stop and TaskCompleted (#43). - **OpenCode guard execution:** replace the unsupported Bun stdin path with real subprocess execution in both APIs. Denial, missing guards, crashes and timeouts block tools. Transactional generation refuses user-owned collisions and symlinked destinations; lifecycle cleanup releases advisory ownership. - **Isolated release smoke:** `scripts/release.sh` installs the packed artifact in a disposable, network-disabled container with no mounts and the OS user's default home. Host settings are not used as a smoke sandbox. ### Changed - Hook entries: 29 to 28 across the same 14 Claude events. Skills remain 116 and agents remain 44. Bats coverage grows from 2012 to 2037 tests. - Refresh compatibility documentation and remove stale inventory counts from the backward-compatibility policy. All protected public surfaces remain. - Skill body budget is unchanged: the largest body remains `design-engineering` at 17,197 bytes, less than 2,000 bytes below the 18,000-byte warning threshold, so the release ratchet does not apply. **Included pull request:** [#43: run the project checker once on Claude Code Stop](https://github.com/softspark/ai-toolkit/pull/43), contributed by @jacekprzybyl. **Full changelog:** https://github.com/softspark/ai-toolkit/compare/v5.4.2...v5.5.0

  5. v5.4.2 GitHub ↗
    原版更新说明

    ## What's Changed * fix(install): read state.json on native Windows by @jacekprzybyl in https://github.com/softspark/ai-toolkit/pull/39 * test: isolate USERPROFILE together with HOME on Windows by @jacekprzybyl in https://github.com/softspark/ai-toolkit/pull/41 **Full Changelog**: https://github.com/softspark/ai-toolkit/compare/v5.4.1...v5.4.2

此处仅展示最近 5 个版本。 在原站查看完整版本历史 ↗

当前收录版本

0.0.1

核对来源版本 ↗

质量评测

满分100 · 来自 SkillPi LAB 实验室的评测报告。

场景价值95/100

亮点

  • 全面覆盖 Swift 编码风格、主流框架(SwiftUI、Combine、SwiftData)、设计模式、安全实践与测试策略
  • 内容深度与广度俱佳,适用于 Swift 开发者的日常编码与代码审查场景
触发精准90/100

亮点

  • 触发词精准覆盖 Swift 开发核心文件类型与技术关键词,边界清晰,误触风险低
结构规范95/100

亮点

  • 文档结构清晰,分章节组织,每节有明确子项与具体示例,frontmatter 完整规范
执行可靠95/100

亮点

  • 内容为静态规则文档,无执行风险,工具引用均为标准库或主流开源库,参数描述准确
证据可验 (前四项均值)94/100

取前四项原始分值的算术平均,四舍五入取整。可通过收录报告核对各项评分与说明,运行记录需单独查证。

正文静态检测

100 / 100 · skillpi-20261123-no-r3-v1

正文未命中当前规则。

分数仅反映正文中的规则命中,供使用前参考。附件与运行行为不在此评分范围内。

权限与使用前检查

根据当前主文档自动整理,命中内容可能是示例或防护说明;不是实际权限声明,也未覆盖附件、外部依赖或运行行为。

未从主文档识别到上述常见操作,不能据此判断无需权限或没有风险。

本站展示提示不会授予权限。涉及文件改动、密钥、外部发送或费用时,仍由你使用的工具按需确认。

安全评级与访问范围

safe · 静态证据核验,不代表运行安全保证。

使用前请由宿主核对并授权实际文件读写、外部服务和联网目的地;本站的核验不会授予任何运行权限。