整理译自: http://aplus.rs/2017/much-ado-about-ios-app-architecture/
项目可维护的一些原则:
尽可能少写代码
不要试图和 SDK 和工具过不去
采用 iOS 框架中, 尤其是 UIKit 中已有的设计模式
MVC, Delegate(代理), Dependency Injection(依赖注入), Target / Action
以 Coordinators(协作者) 的方式扩展 MVC, 并用 coordinatingResponder 处理数据流
让 UIViewControllers 各司其职
用代码写 UI 层违反了原则 1
尽可能用 .storyboard / .xib 构建 UI. 除非确实必要, 不用代码写 UI
不用 .storyboard 处理数据流, 而是仅作为优化的 .xib, 仅基于 1-2 个控制器
除非绝对必要, 否则不要替代系统框架
譬如: https://github.com/mirego/PinLayout , https://github.com/layoutBox/FlexLayout ,LayoutKit http://layoutkit.org/ ,Layout https://github.com/schibsted/layout 等替换 Apple 提供的 AutoLayout.
依托 Swift 的表达能力
尽可能减少第三方依赖, 除非有明确的需求
Crypto https://github.com/onmyway133/Arcane , https://github.com/kishikawakatsumi/KeychainAccess 等基于 C 语言框架的封装库
https://github.com/utahiosmac/Marshal JSON 解析库, Swift 4 支持 Codable, 也可以尝试不依赖了
https://github.com/roberthein/TinyConstraints 微型 AutoLayout 工具库
采用一致的代码风格
将代码元素置于相当的地方
采用一致的顺序
尽可能采用大量一致的 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
当你对于应该写 "聪明的" 还是 "直白的" 代码感到疑惑时?
记住! 一定要保持 "直白".
关注下面的标签, 发现更多相似文章
iOS
架构
设计模式
Swift
安装掘金浏览器插件
来源: https://juejin.im/post/5ac633d15188255cb32e5ead