微云swift编码规范

栏目: Swift · 发布时间: 6年前

内容简介:GoodBad​

Good

func myFunc() {
}

class MyClass() {  
}

Bad

func myFunc() 
{
}

函数、类中间要空一行

Good

func myFunc1() {
}

func myFunc2() {
}

class MyClass1 {
}

Bad

func myFunc1() {
}
func myFunc2() {
}


class MyClass1 {
}

代码逻辑不同块之间,要空一行

Good

func process() {
    // do one thing
    doCode1()
    
    // do another thing
    doAnother()
}

Bad

func process() {
    // do one thing
    doCode1()
    // do another thing
    doAnother()
}

缩进为一个tab (4个空格的宽度)

空行里不能有空的tab、空格

二元运算符,前后都要有空格

Good

let i = 5 + 6
let r = i % 10

Bad

let i=5+6
let r=i%10

区间运算符两边也要有空格

Good

let range = 1 ..< 10

Bad

let range = 1..<10

逗号后面跟空格

Good

let arr = [1, 2, 3, 4]

Bad

let arr = [1,2,3,4]

注释符号,与注释内容之间加空格

Good

print("Hello")	// 打印Hello

Bad

print("Hello")//打印Hello

类继承、参数名和类型之间等,冒号前面不加空格,但后面跟空格

Good

class MyClass: NSObject {
}

func myFunc(value: Int) {
}

Bad

class MyClass : NSObject {
}

func myFunc(value:Int) {
}

自定义操作符,声明及实现,两边都要有空格隔开

Good

infix operator ||| : RxPrecedence
public func ||| <T, U>(lhs: T, rhs: T) -> U {
}

Bad

infix operator |||: RxPrecedence
public func |||<T, U>(lhs: T, rhs: T) -> U {
}

if后面的else,跟着上一个if的右括号

Good

if flag {
    // code
} else {
    // code
}

Bad

if flag
{
    // code
}
else
{
    // code
}

switch中,case跟switch左对齐

Good

switch value {
case 1:
    // code
case 2:
    // code
default:
    // code
}

Bad

switch value {
    case 1:
        // code
    case 2:
        // code
    default:
        // code
}

函数体长度不超过200行

单行不能超过200个字符

单类体长度不超过300行

实现每个协议时,在单独的extension里来实现

Good

class MyViewController: UIViewController {
}

extension MyViewController: UITableViewDataSource {
}

extension MyViewController: UIScrollViewDelegate {
}

Bad

class MyViewController: UIViewController, UITableViewDataSource, UIScrollViewDelegate {
}

闭包中的单表达式,省略return

Good

let r = arr.filter { $0 % 2 == 0 }

Bad

let r = arr.filter { return $0 % 2 == 0 }

简单闭包,写在同一行

Good

let r = arr.filter { $0 % 2 == 0 }

Bad

let r = arr.filter {
    $0 % 2 == 0
}

尾随闭包,在单闭包参数时才使用

Good

// 仅有一个闭包参数,使用尾随闭包写法
let r = arr.filter { $0 % 2 == 0 }

// 有两个闭包参数,则不使用尾随闭包写法
arr.forEach(where: { $0 % 2 == 0 }, body: { print($0) })

Bad

let r = arr.filter({ $0 % 2 == 0 })

arr.forEach(where: { $0 % 2 == 0 }) { print($0) }

闭包声明时,不需要写参数名,只声明类型即可

Good

func myFunc(completion: (Data) -> Void) {
}

Bad

func myFunc(completion: (_ data: Data) -> Void) {
}

使用[weak self]修饰的闭包,闭包开始判断self的有效性

fetchList(param) { [weak self] lst in
    guard let self = self else {
        return
    }
    // code
}

过滤、转换等,优先使用filter、map等高阶函数简化代码

Good

let arr = [1, 2, 3, 4]
let total = arr.reduce(0, +)

Bad

let arr = [1, 2, 3, 4]
var total = 0
for i in arr {
    total += i
}

优先使用let定义变量,而不是var

能推断出来的类型,不需要加类型限定

Good

let str = "Hello"
view.backgroundColor = .red

Bad

let str: String = "Hello"
view.backgroundColor = UIColor.red

变量声明时,使用简化写法。

Good

var m = [Int]()

Bad

var n = Array<Int>()

单行注释,优先使用 //

Good

print("Hello")	// Hello

Bad

print("Hello")	/* Hello */

异常的分支,提前用guard结束。

Good

func process(value: Int) {
    guard value > 0 else {
        return
    }

    // code
}

Bad

func process(value: Int) {
    if value > 0 {
        // code
    }
}

多个嵌套条件,能合并的,就合并到一个if中

Good

func process(v1: Int, v2: Int) {
    if v1 > 0, v2 > 0 {
        // code
    }
}

Bad

func process(v1: Int, v2: Int) {
    if v1 > 0 {
        if v2 > 0 {
            // code
        }
    }
}

尽可能使用private、fileprivate来限制作用域

Good

class MyClass {
    private func util() {   // 仅在类内部使用
    }
}

Bad

class MyClass {
    func util() {   // 仅在类内部使用
    }
}

尽量省略self,必要时才加

Good

extension Array where Element == Int {
    func myFunc() -> Int {
        return filter { $0 > 10 }.count
    }
}

Bad

extension Array where Element == Int {
    func myFunc() -> Int {
        return self.filter { $0 > 10 }.count
    }
}

不使用强制解包

Good

if let value = optional {
    // code
}

Bad

let value = optional!

不使用强制类型转换

Good

if let r = value as? String {
    // code
}

Bad

let r = value as! String

不使用try!

Good

let r = try? decodeData()

Bad

let r = try! decodeData()

不使用隐式解包

Good

let opt: String?

Bad

let opt: String!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

菜鸟侦探挑战数据分析

菜鸟侦探挑战数据分析

[日] 石田基广 / 支鹏浩 / 人民邮电出版社 / 2017-1 / 42

本书以小说的形式展开,讲述了主人公俵太从大学文科专业毕业后进入征信所,从零开始学习数据分析的故事。书中以主人公就职的征信所所在的商业街为舞台,选取贴近生活的案例,将平均值、t检验、卡方检验、相关、回归分析、文本挖掘以及时间序列分析等数据分析的基础知识融入到了生动有趣的侦探故事中,讲解由浅入深、寓教于乐,没有深奥的理论和晦涩的术语,同时提供了大量实际数据,使用免费自由软件RStudio引领读者进一步......一起来看看 《菜鸟侦探挑战数据分析》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

html转js在线工具
html转js在线工具

html转js在线工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具