1. 程式人生 > IOS開發 >一份 iOS 開發實踐檢查清單

一份 iOS 開發實踐檢查清單

回顧即開始

目錄

  1. 開始專案
  2. 實用公共庫
  3. 架構
  4. 資料儲存
  5. 資源
  6. 編碼規範
  7. 安全性
  8. 診斷

開始專案

Xcode

.gitignore

依賴管理

sudo gem install cocoapods # 安裝
pod init # 初始化建立 Podfile
pod install/update # 安裝/更新依賴
複製程式碼
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
└─ ···
複製程式碼
// 全域性常量建議採用 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) } } } 複製程式碼

實用公共庫

架構

Model

Views

Controllers

let fooViewController = FooViewController(withViewModel: fooViewModel)
複製程式碼

資料儲存

  • 避免“回撥地獄”(callback hell)
  • RxSwift 非同步響應式程式設計
func fetchGigs(for artist: Artist) -> Observable<[Gig]> {
    // ...
}
複製程式碼

資源

  • 採用 .pdf 向量圖
  • R.swift 自動集中管理圖片、xib、字串等各項資源
  • ImageOptim 圖片優化

編碼規範

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) {
        // ...
    }
    
}

複製程式碼

安全性

資料儲存

網路

  • 採用 https TLS 加密傳輸

日誌採集

  • 設定正確的日誌輸出等級
  • 線上環境一定不要列印密碼等敏感資訊
  • 記錄基本程式碼控制流,以便除錯

使用者互動

  • UITextField 用於密碼等敏感資訊輸入時設定secureTextEntrytrue
  • 必要時清空剪貼簿等可能存在的敏感資料
    • applicationDidEnterBackground

診斷

  • 重視並儘量解決編譯器警告
  • 靜態分析
    • Product → Analyze
  • 除錯
    • 開啟 Exception 斷點
    • Reveal 檢視除錯
  • 效能剖析

參與貢獻

歡迎指正共建與獲取最新狀態:Github 倉庫 - iOS-Practice-Checklist