- 使用4個空格進行縮進。(可在Xcode的Preferences中進行設(shè)置)
- 左大括號寫在首行。eg:
class TestClass {
/* ... */
}
- 二元運算符前后添加空格。eg:
let testValue = 1 + 2
- 添加有必要的注釋(使用Xcode注釋快捷鍵)。eg:
// 測試按鈕
var testBtn:UIButton
/// <#Description#>
///
/// - Parameter testValue: <#testValue description#>
/// - Returns: <#return value description#>
func testFunction(testValue: Int) -> Int {
/*...*/
}
-
圖片命名要清晰。eg:
與功能相對應命名圖片 - 變量命名應該能推斷出該變量類型,或以變量類型結(jié)尾。eg:
let coverImageView: UIImageView
let firstName: String
-
接口命名與后臺所提供的保持一直,由請求路徑、接口名和請求后綴組成。 eg:
GetVipColors接口.jpg -
接口的請求建議一次封裝多處使用,尤其是像獲取用戶信息這種。eg:
對GetVipColors接口的封裝.jpg - 類型多時使用枚舉,枚舉名字和功能對應 eg:
/// 房間類型
///
/// - common: 普通模式
/// - handPattern: 相親
/// - pkPattern: 團戰(zhàn)
enum RoomShowType:String {
case common = "common"
case handPattern = "hand_pattern"
case pkPattern = "pk_pattern"
}
- 可選型使用guard或者if let解包后使用。eg:
// guard 解包
guard let giftView = giftViewArr[toMicId] else {return}
// if let 解包
if let giftView = giftViewArr[toMicId] {
}
// if let 解包多個
if let subview = subview, let volume = volume {
}
- block的執(zhí)行如果存在延遲,需要用weak來規(guī)避強引用。 eg:
DispatchQueue.main.asyncAfter(deadline: .now() + 2) { [weak self] in
if let weakself = self{
weakself.rainLayer.birthRate = 0
}
}
- 使用// MARK: - XXX進行分組。eg:
/// MARK: - System
// MARK: - Public
// MARK: - Action
// MARK: - Private
// MARK: - xxxDelegate
- 控制代碼段大小(比如有一個100行的代碼塊,其中50行代碼是實現(xiàn)了某個動畫,則把這50行代碼封裝為另一個代碼塊)
- 較大功能或邏輯的修改,最好標記一下。eg:
// modified by FangLin 2018-01-01
- 在取可變數(shù)組中的元素時,要考慮數(shù)組越界的情況。
- 修改后的代碼要進行編譯驗證。


