超全面的Swift编码规范(推荐)

2020-01-08 23:40:30丽君


// 推荐
let firstCondition = x == firstReallyReallyLongPredicateFunction()
let secondCondition = y == secondReallyReallyLongPredicateFunction()
let thirdCondition = z == thirdReallyReallyLongPredicateFunction()
if firstCondition && secondCondition && thirdCondition {
 // 你要干什么
}
// 不推荐
if x == firstReallyReallyLongPredicateFunction()
 && y == secondReallyReallyLongPredicateFunction()
 && z == thirdReallyReallyLongPredicateFunction() {
 // 你要干什么
}

命名

使用帕斯卡拼写法(又名大骆驼拼写法,首字母大写)为类型命名(如struct,enum,class,typedef,associatedtype等)。

使用小骆驼拼写法(首字母小写)为函数,方法,常亮,参数等命名。

首字母缩略词在命名中一般来说都是全部大写,例外的情形是如果首字母缩略词是一个命名的开始部分,而这个命名需要小写字母作为开头,这种情形下首字母缩略词全部小写。


// "HTML" 是变量名的开头, 需要全部小写 "html"
let htmlBodyContent: String = "<p>Hello, World!</p>"
// 推荐使用 ID 而不是 Id
let profileID: Int = 1
// 推荐使用 URLFinder 而不是 UrlFinder
class URLFinder {
 /* ... */
}

使用前缀 k + 大骆驼命名法 为所有非单例的静态常量命名。


class ClassName {
 // 基元常量使用 k 作为前缀
 static let kSomeConstantHeight: CGFloat = 80.0
 // 非基元常量也是用 k 作为前缀
 static let kDeleteButtonColor = UIColor.redColor()
 // 对于单例不要使用k作为前缀
 static let sharedInstance = MyClassName()
 /* ... */
}

命名应该具有描述性个清晰性的。


// 推荐
class RoundAnimatingButton: UIButton { /* ... */ }
// 不推荐
class CustomButton: UIButton { /* ... */ }

不要缩写、简写或单个字母命名。


// 推荐
class RoundAnimatingButton: UIButton {
 let animationDuration: NSTimeInterval
 func startAnimating() {
 let firstSubview = subviews.first
 }
}
// 不推荐
class RoundAnimating: UIButton {
 let aniDur: NSTimeInterval
 func srtAnmating() {
 let v = subviews.first
 }
}

如果原有命名不能明显表明类型,则属性命名内要包括类型信息。


// 推荐
class ConnectionTableViewCell: UITableViewCell {
 let personImageView: UIImageView
 let animationDuration: NSTimeInterval
 // 作为属性名的firstName,很明显是字符串类型,所以不用在命名里不用包含String
 let firstName: String
 // 虽然不推荐, 这里用 Controller 代替 ViewController 也可以。
 let popupController: UIViewController
 let popupViewController: UIViewController
 // 如果需要使用UIViewController的子类,如TableViewController, CollectionViewController, SplitViewController, 等,需要在命名里标名类型。
 let popupTableViewController: UITableViewController
 // 当使用outlets时, 确保命名中标注类型。
 @IBOutlet weak var submitButton: UIButton!
 @IBOutlet weak var emailTextField: UITextField!
 @IBOutlet weak var nameLabel: UILabel!
}
// 不推荐
class ConnectionTableViewCell: UITableViewCell {
 // 这个不是 UIImage, 不应该以Image 为结尾命名。
 // 建议使用 personImageView
 let personImage: UIImageView
 // 这个不是String,应该命名为 textLabel
 let text: UILabel
 // animation 不能清晰表达出时间间隔
 // 建议使用 animationDuration 或 animationTimeInterval
 let animation: NSTimeInterval
 // transition 不能清晰表达出是String
 // 建议使用 transitionText 或 transitionString
 let transition: String
 // 这个是ViewController,不是View
 let popupView: UIViewController
 // 由于不建议使用缩写,这里建议使用 ViewController替换 VC
 let popupVC: UIViewController
 // 技术上讲这个变量是 UIViewController, 但应该表达出这个变量是TableViewController
 let popupViewController: UITableViewController
 // 为了保持一致性,建议把类型放到变量的结尾,而不是开始,如submitButton
 @IBOutlet weak var btnSubmit: UIButton!
 @IBOutlet weak var buttonSubmit: UIButton!
 // 在使用outlets 时,变量名内应包含类型名。
 // 这里建议使用 firstNameLabel
 @IBOutlet weak var firstName: UILabel!
}