一份 iOS 開發實踐檢查清單
阿新 • • 發佈:2019-12-25
回顧即開始
目錄
開始專案
Xcode
.gitignore
-
Git 新增
.gitignore
: Swift or Objective-C
依賴管理
- CocoaPods 文件
sudo gem install cocoapods # 安裝
pod init # 初始化建立 Podfile
pod install/update # 安裝/更新依賴
複製程式碼
- Carthage (Swift) 文件
brew install carthage # 安裝
carthage bootstrap/update # 安裝或更新依賴
複製程式碼
工程目錄結構
- 熟悉並保持合理的目錄結構
AwesomeProject
├─ Assets
│ ├─ Info.Plist
│ ├─ Localizable.strings
│ ├─ R.generated.swift # 可選,R.swift 生成
│ ├─ LaunchScreen.storyboard
│ ├─ Assets.xcassets
│ ├─ ProjectName.entitlements
│ ├─ Info.Plist
│ ├─ BuildConfigs
│ └─ ···
├─Sources
│ ├─ Modules
│ ├─ MyModule
│ │ │ ├─ Models
│ │ │ ├─ Views
│ │ │ └─ Controllers (or ViewModels)
│ │ └─ ···
│ ├─ Stores
│ ├─ Helpers
│ ├─ Utilities
│ ├─ Extentsions
│ ├─ Mediator
│ ├─ Ventors
│ └─ ···
├─Tests
└─ ···
複製程式碼
- 字串本地化(Localization)
- 最小化常量作用域(Constants)
// 全域性常量建議採用 Enum 定義
enum Constants {
static let myConstant = "Just a constant"
}
enum Apprearance {
enum Sizes {
static let gutter: CGFloat = 15
static let cardGutter: CGFloat = 8
···
}
enum Color {
static let primaryColor = UIColor(red: 0.22,green: 0.58,blue: 0.29,alpha: 1.0)
static let secondaryColor = UIColor.lightGray
static let background = UIColor.white
enum Red {
// 視覺化顏色
static let medium = #colorLiteral(red: 0.22,alpha: 1.0)
static let light = #colorLiteral(red: 0.22,alpha: 1.0)
}
}
}
複製程式碼
- Git 分支模型
實用公共庫
-
Alamofire 網路庫
-
Moya 基於Alamofire 封裝的網路抽象層
-
Reachability.swift 用於網路狀況檢查
-
R.swift 自動將各種資源強型別化
-
RxSwift 響應式程式設計框架 by ReactiveX.io
-
LayoutKit 高效能檢視佈局庫
-
Kingfisher 輕量級圖片下載快取庫
-
NSLogger 便捷日誌工具
-
FLEX/DoraemonKit 應用內 Debug 工具庫
架構
- Model-View-Controller (MVC) in iOS: A Modern Approach
- Introduction to MVVM
- MVVM with Coordinators and RxSwift
- ReactiveCocoa and MVVM,an Introduction
Model
-
保持 Model 不可變性,
struct
+Codable
Apple 文件 - SwiftyJSON / Argo [可選]
Views
- 採用 AutoLayout 佈局
Controllers
- 避免控制器臃腫
- 儘量採用依賴注入而不是單例
let fooViewController = FooViewController(withViewModel: fooViewModel)
複製程式碼
資料儲存
- 避免“回撥地獄”(callback hell)
- RxSwift 非同步響應式程式設計
func fetchGigs(for artist: Artist) -> Observable<[Gig]> {
// ...
}
複製程式碼
- CoreData 持久化
資源
-
採用
.pdf
向量圖 - R.swift 自動集中管理圖片、xib、字串等各項資源
- ImageOptim 圖片優化
編碼規範
-
//MARK:
+Extension
分組結構化程式碼
import SomeExternalFramework
class FooViewController : UIViewController {
let foo: Foo
private let fooStringConstant = "FooConstant"
private let floatConstant = 1234.5
// MARK: Lifecycle
// Custom initializers go here
···
}
// MARK: View Lifecycle
extension FooViewController {
override func viewDidLoad() {
super.viewDidLoad()
// ...
}
}
// MARK: Layout
extension FooViewController {
private func makeViewConstraints() {
// ...
}
}
// MARK: User Interaction
extension FooViewController {
func foobarButtonTapped() {
// ...
}
}
// MARK: FoobarDelegate
extension FooViewController: FoobarDelegate {
func foobar(foobar: Foobar,didSomethingWithFoo foo: Foo) {
// ...
}
}
// MARK: Helpers
extension FooViewController {
private func displayNameForFoo(foo: Foo) {
// ...
}
}
複製程式碼
安全性
資料儲存
-
Token、使用者名稱密碼及部分隱私敏感資料避免採用
UserDefault
或CoreData
等非加密持久化方式 -
採用
KeyChain
加密儲存敏感資料
網路
-
採用
https
TLS 加密傳輸
日誌採集
- 設定正確的日誌輸出等級
- 線上環境一定不要列印密碼等敏感資訊
- 記錄基本程式碼控制流,以便除錯
使用者互動
-
UITextField
用於密碼等敏感資訊輸入時設定secureTextEntry
為true
-
必要時清空剪貼簿等可能存在的敏感資料
-
applicationDidEnterBackground
-
診斷
- 重視並儘量解決編譯器警告
-
靜態分析
- Product → Analyze
-
除錯
-
開啟
Exception
斷點 - Reveal 檢視除錯
-
開啟
- 效能剖析
參與貢獻
歡迎指正共建與獲取最新狀態:Github 倉庫 - iOS-Practice-Checklist