1.編碼格式
1.1 使用四個空格進行縮進(在 Xcode > Preferences > Text Editing > Indentation 將 Tab 和自動縮進都設置為 4 個空格)
1.2 每行最多160個字符,這樣可以避免一行過長。 (Xcode->Preferences->Text Editing->Page guide at column: 設置成160即可)
1.3使用二元運算符(+ - * / = > >= < <= ==或->)的前后都需要添加空格,左小括號后面和右小括號前面不需要空格。
// 推薦
let value = (1 + 2) * 3
// 不推薦
let value = ( 1-2 )*3
1.4 相比較于OC,swift不建議在每句代碼的結尾添加分號(;)
// 推薦
let str: String = "hello world!"
// 不推薦
let str: String = "hello world!";
1.5 在逗號(,)、冒號(:)后面加一個空格
// 推薦
let array: Array = [1, 2, 3, 4, 5, 6]
// 不推薦
let array: Array = [1,2,3,4,5,6]
let array:Array = [1, 2, 3, 4, 5, 6]
1.6 方法的左大括號不要另起,并和方法名之間留有空格
// 推薦
func a() {
//代碼邏輯處理
}
// 不推薦
func a()
{
//代碼邏輯處理
}
1.7 判斷語句不用加括號
// 推薦
if typeValue == 1 {
//代碼邏輯處理
}
// 不推薦
if (typeValue == 1) {
//代碼邏輯處理
}
1.8 在訪問枚舉類型時,使用更簡潔的點語法
// 推薦
imageView.setImageWithURL(url, type: .person)
// 不推薦
imageView.setImageWithURL(url, type: AsyncImageView.Type.person)
1.9 針對屬性或方法添加注釋時,盡可能使用Xcode注釋快捷鍵 command + option + /
//屬性
/// <#Description#>
var title: String?
//方法
/// <#Description#>
///
/// - Parameters:
/// - tableView: <#tableView description#>
/// - section: <#section description#>
/// - Returns: <#return value description#>
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataList.count
}
1.10 當函數參數有多個時,應該函數參數分行顯示
// 推薦
init(title: String,
message: String?,
sureBtnTitle: String,
sureBtnTitleColor: UIColor,
sureBtnBlock: AlertBtnBlock?) {
//代碼邏輯處理
}
// 不推薦
init(title: String, message: String?, sureBtnTitle: String, sureBtnTitleColor: UIColor, sureBtnBlock: AlertBtnBlock?) {
//代碼邏輯處理
}
1.11 當遇到需要處理的數組或字典內容較多需要多行顯示
// 推薦
var array: Array = [String]()
array = [
"aaaaaaaaaaaa",
"bbbbbbbbbbbb",
"cccccccccccc"
]
var dic: Dictionary = [String: String]()
dic = [
"a": "aaaaaaaaaa",
"b": "bbbbbbbbbb",
"c": "cccccccccc"
]
// 不推薦
var array: Array = [String]()
array = ["aaaaaaaaaaaa", "bbbbbbbbbbbb", "cccccccccccc"]
var dic: Dictionary = [String: String]()
dic = ["a": "aaaaaaaaaa", "b": "bbbbbbbbbb", "c": "cccccccccc"]
2.命名規范
2.1 常量,變量,函數,方法的命名規則使用小駝峰規則,首字母小寫,類型名使用大駝峰規則,首字母大寫。
class MyClass: class {
let myImageView: UIImageView
let myName: String
}
2.2 當命名里出現縮寫詞時,縮寫詞要么全部大寫,要么全部小寫,以首字母大小寫為準
// 推薦
let urlString: URLString
// 不推薦
let uRLString: UrlString
2.3 bool類型命名時,使用is作為前綴
let isMine: Bool = false
let isFirstLogin: Bool = true
2.4 屬性命名應該要包括類型信息
// 推薦
let titleLab: UILabel = UILabel()
let iconImageView: UIImageView = UIImageView()
let iconImgV: UIImageView = UIImageView()
let homePageVC: UIViewController = UIViewController()
let sureBtn: UIButton = UIButton.init(type: .custom)
// 不推薦
let title: UILabel = UILabel()
let iconImage: UIImageView = UIImageView()
let homePageView: UIViewController = UIViewController()
let btnSubmit: UIButton = UIButton.init(type: .custom)
3.語法規范
3.1 盡可能的多使用let,少使用var
3.2 Switch 模塊中不顯式使用break
3.3 可選類型拆包取值時,使用if let 判斷
// 推薦
if let data = result.data {
print(data)
}
// 不推薦
if result.data != nil {
print(result.data)
}
3.4常量定義,建議盡可能定義在類型里面,避免污染全局命名空間
// 推薦
class HomeListCell: UITableViewCell {
static let kHomeCellHeight = 80.0
}
// 不推薦
static let kHomeCellHeight = 80.0
class HomeListCell: UITableViewCell {
//代碼邏輯處理
}
3.5 循環遍歷使用for-in表達式
//循環
for i in 0..<list.count {
print(i)
}
//遍歷
for listData in listArray {
print(listData)
}
3.6 建議把訪問修飾符放到第一個位置
// 推薦
private static let kMyPrivateNumber: Int
// 不推薦
static private let kMyPrivateNumber: Int
3.7 訪問修飾符不應單獨另起一行,應和訪問修飾符描述的對象保持在同一行
// 推薦
public class Pirate {
//代碼邏輯處理
}
// 不推薦
public
class Pirate {
//代碼邏輯處理
}
3.8 聲明單例屬性可以通過下面方式進行
class PirateManager {
static let sharedInstance = PirateManager()
}