【iOS印象】漫谈 iOS App 架构与设计模式

2,827 阅读1分钟

项目可维护的一些原则:
  1. 尽可能少写代码
  2. 不要试图和 SDK 和工具过不去
    • 采用 iOS 框架中,尤其是 UIKit 中已有的设计模式
    • MVC, Delegate(代理), Dependency Injection(依赖注入), Target / Action
    • 以 Coordinators(协作者)的方式扩展 MVC,并用 coordinatingResponder 处理数据流
    • 让 UIViewControllers 各司其职
    • 用代码写 UI 层违反了原则1
    • 尽可能用 .storyboard / .xib 构建 UI。除非确实必要,不用代码写 UI
    • 不用 .storyboard 处理数据流,而是仅作为优化的 .xib,仅基于 1-2 个控制器
  3. 除非绝对必要,否则不要替代系统框架
  4. 依托 Swift 的表达能力
  5. 尽可能减少第三方依赖,除非有明确的需求
  6. 采用一致的代码风格
    • 将代码元素置于相当的地方
    • 采用一致的顺序
    • 尽可能采用大量一致的 MARK: - 进行组织
      • Delegates
      • Init / deinit
      • UI outlets
      • Dependencies (if any)
      • Local data source
      • Embedded Controllers (if any)
      • Notification tokens
      • Commented-out list of coordinatorResponder methods that particular file is overriding
      • Any additional stuff

当你对于应该写“聪明的”还是“直白的”代码感到疑惑时?
记住!一定要保持“直白”。