美文网首页
制定自己的Swift编码规范

制定自己的Swift编码规范

作者: 三娃头很铁 | 来源:发表于2018-02-21 22:06 被阅读0次

警告

尽量将所有警告都处理掉。

命名

1.使用具有描述性的名称,
2.使用驼峰式命名规则给类方法和变量等命名。
3.类别名称(类,结构体,枚举和协议)首字母大写,方法或者变量的首字母小写。

// 常量
private let maximumWidgetCount = 100

// 方法
func setupWidget() ->{
    print("ok")
}

// 类
class WidgetContainer {
    var widgetButton: UIButton  
    let  widgetHeightPercentage = 0.85
}

缩进

1.使用四个空格进行缩进
2.每行最多160个字符,这样可以避免一行过长。
3.确保每个文件结尾都有空白行。
4.代码结尾不要使用分号;
5.在逗号后面加一个空格

let numberArray = [1, 3, 6, 10]

6.不要使用 as! 或 try!
推荐

// 使用if let as?判断
if let text = text as? String {
    /* ... */
}

// 使用if let try 或者 try?
if let test = try aTryFuncton() {
    /* ... */
}

Closure Expressions(闭包表达式)
闭包放在最后面

// Preferred(推荐)
UIView.animateWithDuration(1.0) {
  self.myView.alpha = 0
}

UIView.animateWithDuration(1.0,
  animations: {
    self.myView.alpha = 0
  },
  completion: { finished in
    self.myView.removeFromSuperview()
  }
)

// Not Preferred(不推荐)
UIView.animateWithDuration(1.0, animations: {
  self.myView.alpha = 0
})

UIView.animateWithDuration(1.0,
  animations: {
    self.myView.alpha = 0
  }) { f in
    self.myView.removeFromSuperview()
}

7.命名应该具有描述性 和 清晰的。
不要缩写,简写命名,或用单个字母命名。
如果原有命名不能明显表明类型,则属性命名内要包括类型信息。

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

// 推荐
class ConnectionTableViewCell: UITableViewCell {
    //这个不是 UIImage, 不应该以Image 为结尾命名。
    ✅let personImageView: UIImageView
    ❌let personImage: UIImageView 
   
    // 建议使用 animationDuration 或 animationTimeInterval
    ✅let animationDuration: NSTimeInterval
    ❌let animation: NSTimeInterval

    // transition 不能清晰表达出是String
    ✅let transitionText: String
    ✅let transitionString: String
    ❌let transition: String

    // 作为属性名的firstName,很明显是字符串类型,所以不用在命名里不用包含String
    ✅let firstName: String
    ✅let firstNameLabel: UILabel

    // 因用snapkit来布局,页面默认都是ViewController, 所以这个简化
    ✅let testController: UIViewController

    // 当使用outlets时, 确保命名中标注类型。为了保持一致性,建议把类型放到变量的结尾,而不是开始,如submitButton
    ✅@IBOutlet weak var submitButton: UIButton!
    ✅@IBOutlet weak var emailTextField: UITextField!
    ✅@IBOutlet weak var nameLabel: UILabel!

    // 常量统一用全大写,下划线连线
    enum Dict: String {
        case AAA_BBB
        case CCC_DDD
    }

    // Localizable.strings
    "ALERT_TITLE" = "测试";
    "ALERT_CONTENT_UNLOGIN" = "请登录";
    "ALERT_ACTION_OK" = "好的";
    "ALERT_ACTION_CANCEL" = "取消";
}

相关文章

  • 制定自己的Swift编码规范

    警告 尽量将所有警告都处理掉。 命名 1.使用具有描述性的名称,2.使用驼峰式命名规则给类方法和变量等命名。3.类...

  • Swift代码规范

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

  • Swift 代码规范

    Swift 编码规范 A guide to our Swift style and conventions. 按大...

  • 制定自己的前端编码规范

    一、代码缩进:tab键设置四个空格 二、页面的第一行添加标准模式声明 三、html...

  • 《从零开始学Swift》学习笔记 (Day 58)—— Swif

    《从零开始学Swift》学习笔记 (Day 58)—— Swift编码规范之变量或常量声明规范原创文章,欢迎转载。...

  • iOS 代码规范文档

    iOS 代码规范文档 [toc] 修订 概述 制定目的:制定iOS 编码规范,主要是为了规范公司内部的iOS 代码...

  • swift编码规范

    Swift 编码规范 基本原则 参考资料 通用规则 格式 命名 编码风格 访问修饰符 Enum Optional ...

  • Swift编码规范

    A guide to our Swift style and conventions. This is an at...

  • Swift编码规范

    1 命名 使用驼峰式给类,方法或者变量等。类命名必须大写,然后方法名和变量应该以小写字母开始。 eg 首选的 pr...

  • Swift 编码规范

    温德里奇规范 本规范原文链接. 规范只是一些最佳实践, 请酌情使用. 1 正确性 第一条原则, 也是最根本的原则:...

网友评论

      本文标题:制定自己的Swift编码规范

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