moai-lang-swift

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Swift 6+ Development Specialist

Swift 6+ 开发专家

Swift 6.0+ development expert for iOS/macOS with SwiftUI, Combine, and Swift Concurrency.
Auto-Triggers: Swift files (
.swift
), iOS/macOS projects, Xcode workspaces
专注于iOS/macOS平台的Swift 6.0+开发专家,精通SwiftUI、Combine和Swift Concurrency。
自动触发场景:Swift文件(
.swift
)、iOS/macOS项目、Xcode工作区

Quick Reference

快速参考

Core Capabilities

核心能力

  • Swift 6.0: Typed throws, complete concurrency, data-race safety by default
  • SwiftUI 6: @Observable macro, NavigationStack, modern declarative UI
  • Combine: Reactive programming with publishers and subscribers
  • Swift Concurrency: async/await, actors, TaskGroup, isolation
  • XCTest: Unit testing, UI testing, async test support
  • Swift Package Manager: Dependency management
  • Swift 6.0: Typed throws、完整并发支持、默认数据竞争安全
  • SwiftUI 6: @Observable宏、NavigationStack、现代声明式UI
  • Combine: 基于发布者和订阅者的响应式编程
  • Swift Concurrency: async/await、actors、TaskGroup、隔离机制
  • XCTest: 单元测试、UI测试、异步测试支持
  • Swift Package Manager: 依赖管理

Version Requirements

版本要求

  • Swift: 6.0+
  • Xcode: 16.0+
  • iOS: 17.0+ (recommended), minimum 15.0
  • macOS: 14.0+ (recommended)
  • Swift: 6.0+
  • Xcode: 16.0+
  • iOS: 推荐17.0+,最低15.0
  • macOS: 推荐14.0+

Project Setup

项目配置

Package.swift Configuration: Begin with swift-tools-version comment set to 6.0. Import PackageDescription. Define let package with Package initializer. Set name, platforms array with .iOS and .macOS minimum versions, products array with library definitions, dependencies array with package URLs and version requirements, and targets array with target and testTarget entries including dependencies.
Package.swift配置:以设置为6.0的swift-tools-version注释开头。导入PackageDescription。使用Package初始化器定义let package。设置名称、包含.iOS和.macOS最低版本的platforms数组、包含库定义的products数组、包含包URL和版本要求的dependencies数组,以及包含target和testTarget条目(含依赖项)的targets数组。

Essential Patterns

核心开发模式

Basic @Observable ViewModel: Import Observation framework. Apply @Observable and @MainActor attributes to final class. Declare private(set) var properties for state. Create async functions that set isLoading true, use defer to set false, and assign fetched data with try? await and nil coalescing.
Basic SwiftUI View: Define struct conforming to View. Declare @State private var for viewModel. In body computed property, use NavigationStack containing List iterating over viewModel items. Add .task modifier calling await on viewModel.load and .refreshable modifier for pull-to-refresh.
Basic Actor for Thread Safety: Define actor type with private dictionary for cache. Create get function returning optional Data for key lookup. Create set function taking key and data parameters for cache storage.
基础@Observable ViewModel:导入Observation框架。为最终类添加@Observable和@MainActor属性。声明private(set) var类型的状态属性。创建异步函数,将isLoading设为true,使用defer将其设为false,并通过try? await和nil合并运算符分配获取的数据。
基础SwiftUI视图:定义遵循View协议的结构体。声明@State private var类型的viewModel属性。在body计算属性中,使用NavigationStack包裹遍历viewModel项的List。添加.task修饰符调用await viewModel.load,以及.refreshable修饰符实现下拉刷新。
线程安全基础Actor:定义actor类型,包含用于缓存的私有字典。创建根据键查找返回可选Data的get函数。创建接受键和数据参数用于缓存存储的set函数。

Module Index

模块索引

Swift 6 Features

Swift 6特性

modules/swift6-features.md
  • Typed throws for precise error handling
  • Complete concurrency checking
  • Data-race safety by default
  • Sendable conformance requirements
modules/swift6-features.md
  • Typed throws精确错误处理
  • 完整并发检查
  • 默认数据竞争安全
  • Sendable一致性要求

SwiftUI Patterns

SwiftUI开发模式

modules/swiftui-patterns.md
  • @Observable macro and state management
  • NavigationStack and navigation patterns
  • View lifecycle and .task modifier
  • Environment and dependency injection
modules/swiftui-patterns.md
  • @Observable宏与状态管理
  • NavigationStack与导航模式
  • 视图生命周期与.task修饰符
  • 环境与依赖注入

Swift Concurrency

Swift并发

modules/concurrency.md
  • async/await fundamentals
  • Actor isolation and @MainActor
  • TaskGroup for parallel execution
  • Custom executors and structured concurrency
modules/concurrency.md
  • async/await基础
  • Actor隔离与@MainActor
  • TaskGroup并行执行
  • 自定义执行器与结构化并发

Combine Framework

Combine框架

modules/combine-reactive.md
  • Publishers and Subscribers
  • Operators and transformations
  • async/await bridge patterns
  • Integration with SwiftUI
modules/combine-reactive.md
  • 发布者与订阅者
  • 操作符与转换
  • async/await桥接模式
  • 与SwiftUI的集成

Context7 Library Mappings

Context7库映射

Core Swift

Swift核心库

  • /apple/swift
    - Swift language and standard library
  • /apple/swift-evolution
    - Swift evolution proposals
  • /apple/swift-package-manager
    - SwiftPM documentation
  • /apple/swift-async-algorithms
    - Async sequence algorithms
  • /apple/swift
    - Swift语言及标准库
  • /apple/swift-evolution
    - Swift演进提案
  • /apple/swift-package-manager
    - SwiftPM文档
  • /apple/swift-async-algorithms
    - 异步序列算法

Popular Libraries

热门库

  • /Alamofire/Alamofire
    - HTTP networking
  • /onevcat/Kingfisher
    - Image downloading and caching
  • /realm/realm-swift
    - Mobile database
  • /pointfreeco/swift-composable-architecture
    - TCA architecture
  • /Quick/Quick
    - BDD testing framework
  • /Quick/Nimble
    - Matcher framework
  • /Alamofire/Alamofire
    - HTTP网络库
  • /onevcat/Kingfisher
    - 图片下载与缓存
  • /realm/realm-swift
    - 移动数据库
  • /pointfreeco/swift-composable-architecture
    - TCA架构
  • /Quick/Quick
    - BDD测试框架
  • /Quick/Nimble
    - 匹配器框架

Testing Quick Start

测试快速入门

Async Test with MainActor: Apply @MainActor attribute to test class extending XCTestCase. Define test function with async throws. Create mock API and set mock data. Initialize system under test with mock. Call await on async method. Use XCTAssertEqual for count verification and XCTAssertFalse for boolean state checks.
基于MainActor的异步测试:为继承XCTestCase的测试类添加@MainActor属性。定义async throws类型的测试函数。创建模拟API并设置模拟数据。使用模拟对象初始化被测系统。调用异步方法的await。使用XCTAssertEqual验证计数,使用XCTAssertFalse验证布尔状态。

Works Well With

协同工具

  • moai-lang-kotlin
    - Android counterpart for cross-platform projects
  • moai-lang-flutter
    - Flutter/Dart for cross-platform mobile
  • moai-domain-backend
    - API integration and backend communication
  • moai-foundation-quality
    - iOS security best practices
  • moai-workflow-testing
    - Xcode debugging and profiling
  • moai-lang-kotlin
    - 跨平台项目的Android端对应工具
  • moai-lang-flutter
    - 跨平台移动开发的Flutter/Dart工具
  • moai-domain-backend
    - API集成与后端通信工具
  • moai-foundation-quality
    - iOS安全最佳实践工具
  • moai-workflow-testing
    - Xcode调试与性能分析工具

Resources

资源

  • reference.md - Architecture patterns, network layer, SwiftData
  • examples.md - Production-ready code examples
  • reference.md - 架构模式、网络层、SwiftData
  • examples.md - 生产级代码示例