CodeRoadMap
首页路线题库资源库文章社区
CodeRoadMap

程序员的学习成长路线图。内容免费开放,登录后可同步路线图与课时进度。

由 程序员柏风 独立维护 · 关于作者

学习

  • 路线图
  • 课程
  • 面试题
  • 文章
  • 我

更多

  • 资源
  • 社区
  • 关于作者
  • 内容说明

© 2026 CodeRoadMap

津ICP备2026012044号-1·津公网安备12011002026155号·coderoadmap@126.com
iOS 开发入门/课时/架构模式
RawBlame
课时目录12 节

iOS 开发入门

  1. 01iOS 生态与 Xcode
  2. 02Swift 语言基础
  3. 03UIKit 与 Interface Builder
  4. 04SwiftUI 入门
  5. 05架构模式
  6. 06数据持久化
  7. 07网络与 API
  8. 08并发与响应式
  9. 09依赖管理与 CI
  10. 10测试与调试
  11. 11上架与 ASO
  12. 12综合练习

架构模式

MVC、MVVM、VIPER、Delegate。

35 分钟

架构模式

iOS 项目常见 MVC、MVVM、VIPER 与 Coordinator 模式。对照 /roadmaps/ios「Architecture」节点:选对架构让 View 薄、业务可测、依赖可替换。

学习目标

  • 理解 UIKit MVC 的 Massive ViewController 问题
  • 用 MVVM + Combine/async 分离 View 与业务逻辑
  • 了解 Delegate、Coordinator 解耦导航
  • 对照 Spring 分层与 React 组件架构

核心概念

MVC 与 MVVM

UIKit 默认 MVC:ViewController 往往承担过多职责(网络、布局、导航),俗称 Massive ViewController。

MVVM 将 UI 状态与业务分离:

import Foundation
import Combine

// Model
struct Track: Identifiable, Codable {
    let id: Int
    var title: String
    var lessonCount: Int
}

// ViewModel
@MainActor
final class TrackListViewModel: ObservableObject {
    @Published var tracks: [Track] = []
    @Published var isLoading = false
    @Published var errorMessage: String?

    private let repository: TrackRepository

    init(repository: TrackRepository = TrackRepository()) {
        self.repository = repository
    }

    func loadTracks() async {
        isLoading = true
        errorMessage = nil
        do {
            tracks = try await repository.fetchAll()
        } catch {
            errorMessage = error.localizedDescription
        }
        isLoading = false
    }
}

// Repository(数据层,类似 Spring Repository)
protocol TrackRepositoryProtocol {
    func fetchAll() async throws -> [Track]
}

final class TrackRepository: TrackRepositoryProtocol {
    func fetchAll() async throws -> [Track] {
        // URLSession 或 Core Data 实现
        try await Task.sleep(nanoseconds: 500_000_000)
        return [
            Track(id: 1, title: "iOS 入门", lessonCount: 12),
            Track(id: 2, title: "Spring Boot", lessonCount: 20),
        ]
    }
}

SwiftUI View 只订阅 ViewModel 的 @Published 属性,类似 React 组件订阅 Hook 返回的状态。

Delegate 模式

protocol LessonSelectionDelegate: AnyObject {
    func didSelectLesson(_ lesson: Lesson)
}

class LessonListViewController: UIViewController {
    weak var delegate: LessonSelectionDelegate?
}

Delegate 是 iOS 经典解耦方式:UITableViewDelegate、URLSessionDelegate 等均基于此。weak 避免循环引用。

Coordinator 导航

Coordinator 接管 ViewController 间的 push / present 逻辑,ViewController 不再互相引用,便于单元测试与模块化。

与 Java/Spring 或 React 对照

iOS MVVMSpring BootReact
View (SwiftUI)Thymeleaf / 无(API)组件 JSX
ViewModelService 层 DTO + 编排Custom Hook / 组件 state
Repository@Repository / JPAAPI 模块 / React Query
Coordinator / Router—React Router
Delegate回调接口 / EventListenerprops callback

Spring 三层 Controller → Service → Repository 与 iOS View → ViewModel → Repository 结构平行;React 常把 ViewModel 逻辑放在 Custom Hook 中。

练习

  • 为 Track 列表实现 MVVM:ViewModel + Mock Repository
  • 用 Protocol 定义 Repository,便于测试注入
  • 对照 spring-boot-essentials 画分层对照图

小结

MVVM + Repository 是 SwiftUI 时代主流;Delegate 解耦回调。 下一课数据持久化。见 ios-essentials。

加载本节练习…
← 上一课SwiftUI 入门下一课 →数据持久化