AgentSkillsCN

ios-architecture

SwiftUI 项目结构。当您需要创建新项目、开发新功能、设计新界面、合理规划文件存放位置、规范文件命名、优化文件夹组织、进行代码重构、分离 ViewModel、遵循标准目录结构、践行 MVVM 或 Clean Architecture 时,可参考此指南。

SKILL.md
--- frontmatter
name: ios-architecture
description: |
  SwiftUI project structure. Use when creating new projects, new features, new screens, file placement, file naming, folder organization, code refactoring, ViewModel separation, standard directory structure, MVVM, Clean Architecture.
allowed-tools: Read, Grep, Glob, Bash

SwiftUI MVVM Structure

Table of Contents


1. Directory Structure

code
ProjectName/
├── App/
│   └── ProjectNameApp.swift
├── Core/
│   ├── Extensions/
│   ├── Utilities/
│   └── Network/
├── Features/
│   └── FeatureName/
│       ├── Views/
│       ├── ViewModels/
│       ├── Models/
│       └── Services/
├── .claude/shared/
│   ├── Components/
│   ├── Styles/
│   └── Modifiers/
├── Resources/
└── Tests/

Folder Purposes

FolderPurpose
App/App entry point, app-level configuration
Core/Shared code across app (Extensions, Network, Utilities)
Features/Feature modules (Authentication, Home, Profile...)
.claude/shared/Reusable UI components (Buttons, Cards, Modifiers)
Resources/Assets, Localizable, Info.plist
Tests/Unit tests, UI tests

2. Naming Conventions

Folder Naming

  • Features/ - Feature modules by functionality
  • Core/ - App-wide shared code
  • .claude/shared/ - Reusable UI components
  • Services/ - Business logic, API services
  • Resources/ - Assets, Localizable, Info.plist

File Naming

TypeConventionExample
View[Feature]View.swiftLoginView.swift
ViewModel[Feature]ViewModel.swiftLoginViewModel.swift
Model[EntityName].swiftUser.swift
Service[Name]Service.swiftAuthService.swift
Extension[Type]+Extensions.swiftString+Extensions.swift
Component[DescriptiveName].swiftPrimaryButton.swift
Protocol[Name]Protocol.swiftAuthServiceProtocol.swift

Component Naming

TypeLocationExample
Button.claude/shared/Components/Buttons/PrimaryButton.swift
Input.claude/shared/Components/Inputs/PrimaryTextField.swift
Card.claude/shared/Components/Cards/ItemCard.swift
Modal.claude/shared/Components/Modals/ConfirmationModal.swift
Feedback.claude/shared/Components/Feedback/LoadingView.swift

3. Organization Rules

MVVM Responsibilities

LayerResponsibilityContains
ViewUI only, no logicSwiftUI views, layout, styling
ViewModelState + business logic@Published properties, methods
ModelData structure onlyStructs, Codable, Identifiable
ServiceAPI + data persistenceNetwork calls, local storage

Rules

  1. Each feature is a separate folder in Features/
  2. Each feature has subfolders: Views/, ViewModels/, Models/, Services/
  3. View contains UI only, no business logic
  4. ViewModel contains state and business logic
  5. Model is data structure only
  6. Service handles API and data persistence
  7. Components in .claude/shared/ are reusable across features

Dependencies Direction

code
View → ViewModel → Service → Model
  ↓         ↓          ↓
  UI      Logic      Data
  • View depends on ViewModel
  • ViewModel depends on Service
  • Service depends on Model
  • Model has no dependencies

4. Feature Example

Authentication Feature

code
Features/
└── Authentication/
    ├── Views/
    │   ├── LoginView.swift
    │   ├── RegisterView.swift
    │   └── Components/
    │       ├── AuthHeader.swift
    │       └── SocialLoginButtons.swift
    ├── ViewModels/
    │   ├── LoginViewModel.swift
    │   └── RegisterViewModel.swift
    ├── Models/
    │   ├── User.swift
    │   ├── LoginRequest.swift
    │   └── LoginResponse.swift
    └── Services/
        ├── AuthService.swift
        └── AuthServiceProtocol.swift

File Contents Overview

FileContains
LoginView.swiftUI layout, bindings to ViewModel
LoginViewModel.swift@Published state, login(), validate()
User.swiftUser struct with Codable
AuthService.swiftAPI calls for login, register

Checklist

When creating new feature:

  • Create folder in Features/[FeatureName]/
  • Create subfolders: Views/, ViewModels/, Models/, Services/
  • Follow naming conventions
  • Keep View logic-free
  • Keep Model data-only

When creating new screen:

  • Create [Name]View.swift in Views/
  • Create [Name]ViewModel.swift in ViewModels/
  • Create models if needed in Models/
  • Add Preview for View

When creating reusable component:

  • Place in .claude/shared/Components/[Type]/
  • Make it configurable via parameters
  • Add Preview
  • Document usage