1 二元運算符(+, ==, 或->)的前後都需要添加空格
推薦
let testValue = 1 + 2
if testValue == 1 {
/* ... */
}
func testFunction(with testValue: TestClass) -> returnValue {
/* ... */
}
2 一般情況下,在逗號後面加一個空格
推薦
let testArray = [1, 2, 3, 4, 5]
不推薦
let testArray = [1,2,3,4,5]
3 每個文件結尾留一行空行
4 代碼結尾不要使用分號;
推薦
print("Hello World")
不推薦
print("Hello World");
5 左大括號不要另起一行
推薦
// 1. Class Define
class TestClass {
/* ... */
}
// 2. if
if testValue == 1 {
/* ... */
}
// 3. if else
if testValue == 1 {
/* ... */
} else {
/* ... */
}
// 4. while
while isTrue {
/* ... */
}
// 5. guard
guard let testValue = testValue else {
/* ... */
}
不推薦
// 1. Class Define
class TestClass
{
/* ... */
}
// 2. if
if testValue == 1
{
/* ... */
}
// 3. if else
if testValue == 1
{
/* ... */
}
else
{
/* ... */
}
// 4. while
while isTrue
{
/* ... */
}
// 5. guard
guard let testValue = testValue else
{
/* ... */
}
6 判斷語句不用括號
推薦
if testValue == 1 {
/* ... */
}
if testValue == 1 && testString == "test" {
/* ... */
}
不推薦
if (testValue == 1) {
/* ... */
}
if ((testValue == 1) && (testString == "test")) {
/* ... */
}