分号/Semicolons

Swift does not require a semicolon after each statement in your code. They are only required if you wish to combine multiple statements on a single line.

在 Swift 中,每条代码语句后面不需要加分号。只有在一行中拼接多条语句时,才需要加分号。

Do not write multiple statements on a single line separated with semicolons.

尽量避免在一行内写多个语句。

推荐(Preferred):

let swift = "not a scripting language"

不推荐(Not Preferred):

let swift = "not a scripting language";

NOTE: Swift is very different from JavaScript, where omitting semicolons is generally considered unsafe

注:Swift 非常不同于 JavaScript。在 JavaScript 中忽略分号 一般被认为是不安全的

括号/Parentheses

Parentheses around conditionals are not required and should be omitted.

条件周围的括号是不必要的,应该被忽略。

推荐(Preferred):

if name == "Hello" {
  print("World")
}

不推荐(Not Preferred):

if (name == "Hello") {
  print("World")
}

In larger expressions, optional parentheses can sometimes make code read more clearly.

在更大的表达式中,额外的括号有时可以让代码读起来更清晰。

推荐(Preferred):

let playerMark = (player == current ? "X" : "O")

Made with in Shangrao,China By 老雷

Copyright © devler.cn 1987 - Present

赣ICP备19009883号-1