美文网首页
swift 代码规范

swift 代码规范

作者: Chevee | 来源:发表于2019-05-06 14:49 被阅读0次

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()
}

相关文章

  • Raywenderlich 的 Swift 代码规范

    Raywenderlich 的 Swift 代码规范

  • Swift代码规范

    团队的Swift代码规范,参考Swift Style Guide和Swift 4.0 编码规范,并根据团队实际需要...

  • 常用技术网址

    代码规范: 代码规范 网站: CocoaChina Apple文档 swift.gg code4app stack...

  • Swift 代码规范中文-Swift Style Guide

    Swift Style Guide Swift代码规范指南 Make sure to read Apple's A...

  • 一篇详细的swift代码规范

    【转载】转自卓大大翻译谷歌的swift代码规范 [译] 官方 Swift API 设计规范 官方地址:API de...

  • swift代码规范

    代码规范整理 安装swifLint http://www.cocoachina.com/ios/20170602/...

  • Swift代码规范

    当你试图解决一个别人代码中的问题时,难得不是怎么解决这个问题,而是先得找到、读懂这段代码。 推荐文档:https:...

  • Swift代码规范

    推荐个规范代码的库SwiftLint, 有兴趣的同学可瞧瞧如何。相信我,关于代码规范,你需要仔细看看这篇文章最详尽...

  • Swift代码规范

  • Swift 代码规范

    目录 源文件基础知识文件名文件编码空白字符特殊转义序列不可见的字符和修饰符字符串字面量不可见的字符和修饰符 源文件...

网友评论

      本文标题:swift 代码规范

      本文链接:https://www.haomeiwen.com/subject/menroqtx.html