4
4
5
5
## [ 原版戳这里] ( https://github.com/github/swift-style-guide )
6
6
7
- 哪里不对或者不准确的,若能指出 (我把原文贴上来了,点击左侧三角形就能展开用来对照 2015-12-13)
8
- 感激不尽~~~
7
+ 哪里不对或者不准确的,(我把原文贴上来了,点击左侧三角形就能展开用来对照 2015-12-13)
8
+ 若能指出, 感激不尽~~~
9
9
10
10
如果没能及时更新,可能比较忙,或者比较懒 →_ →
11
11
翻译后可以 email 或 ` pull request `
17
17
A guide to our Swift style and conventions.
18
18
</details ></h1 >
19
19
20
- <details ><summary >本文尝试做到以下几点(大概的先后顺序) :</summary >
20
+ <details ><summary >按大概的先后顺序, 本文尝试做到以下几点:</summary >
21
21
This is an attempt to encourage patterns that accomplish the following goals (in
22
22
rough priority order):
23
23
</details >
@@ -37,22 +37,22 @@ then open a pull request. :zap:
37
37
38
38
----
39
39
40
- <h4 ><details ><summary >留空白</summary >Whitespace</details ></h4 >
40
+ <h2 ><details ><summary >留空白</summary >Whitespace</details ></h2 >
41
41
42
42
<ul >
43
- <li ><details ><summary >用 Tabs ,而非 空格</summary >Tabs, not spaces.</details ></li >
43
+ <li ><details ><summary >用 tab ,而非 空格</summary >Tabs, not spaces.</details ></li >
44
44
<li ><details ><summary >文件结束时留一空行</summary >End files with a newline.</details ></li >
45
45
<li ><details ><summary >用足够的空行把代码分割成合理的块</summary >Make liberal use of vertical whitespace to divide code into logical chunks.</details ></li >
46
46
<li ><details ><summary >不要在一行结尾留下空白</summary >Don’t leave trailing whitespace.</details >
47
47
<ul><li><details><summary>千万别在空行留下缩进</summary>Not even leading indentation on blank lines.</details></li></ul>
48
48
</li >
49
49
</ul >
50
50
51
- <h4 ><details >
51
+ <h2 ><details >
52
52
<summary >能用 <code >let</code > 尽量用 <code >let</code > 而不是 <code >var</code ></summary >
53
53
54
54
Prefer ` let ` -bindings over ` var ` -bindings wherever possible
55
- </details ></h4 >
55
+ </details ></h2 >
56
56
57
57
<details >
58
58
<summary >
@@ -70,33 +70,35 @@ Use `let foo = …` over `var foo = …` wherever possible (and when in doubt).
70
70
_ Rationale:_ The intent and meaning of both keywords is clear, but * let-by-default* results in safer and clearer code.
71
71
</details >
72
72
73
+ <br />
74
+
73
75
<details >
74
76
<summary >
75
- <code >let</code >-有保障 并且它的值的永远不会变对程序猿也是个 <i >清晰的标记</i >,对于它的用法 ,之后的代码可以做个强而有力的推断。</summary >
77
+ <code >let</code > 保障它的值的永远不会变,对程序猿也是个 <i >清晰的标记</i >。因此对于它的用法 ,之后的代码可以做个强而有力的推断。</summary >
76
78
77
79
A ` let ` -binding guarantees and * clearly signals to the programmer* that its value will never change. Subsequent code can thus make stronger assumptions about its usage.
78
80
</details >
79
81
80
82
<details >
81
83
<summary >
82
- 猜测代码更容易了 。不然一旦你用了 <code >var</code >,还要去推测值会不会变,这时候你就不得不人肉去检查。
84
+ 理解代码也更容易了 。不然一旦你用了 <code >var</code >,还要去推测值会不会变,这时候你就不得不人肉去检查。
83
85
</summary >
84
86
85
87
It becomes easier to reason about code. Had you used ` var ` while still making the assumption that the value never changed, you would have to manually check that.
86
88
</details >
87
89
88
90
<details >
89
91
<summary >
90
- 这样 ,无论何时你看到 <code >var</code >,就假设它会变,并问自己为啥。
92
+ 相应地 ,无论何时你看到 <code >var</code >,就假设它会变,并问自己为啥。
91
93
</summary >
92
94
93
95
Accordingly, whenever you see a ` var ` identifier being used, assume that it will change and ask yourself why.
94
96
</details >
95
97
96
- <h4 ><details ><summary >
98
+ <h2 ><details ><summary >
97
99
尽早地 <code >return</code > 或者 <code >break</code ></summary >
98
100
99
- Return and break early</details ></h4 >
101
+ Return and break early</details ></h2 >
100
102
101
103
<details >
102
104
<summary >当你遇到某些操作需要通过条件判断去执行,应当尽早地退出判断条件:你不应该用下面这种写法</summary >
@@ -133,9 +135,9 @@ You can also do it with `if` statement, but using `guard` is prefered
133
135
because ` guard ` statement without ` return ` , ` break ` or ` continue ` produces a compile-time error, so exit is guaranteed.
134
136
</details >
135
137
136
- <h4 ><details ><summary >避免对 可选类型 强解包</summary >Avoid Using Force-Unwrapping of Optionals</details ></h4 >
138
+ <h2 ><details ><summary >避免对 可选类型 强解包</summary >Avoid Using Force-Unwrapping of Optionals</details ></h2 >
137
139
138
- <details ><summary >如果你有个 <code >FooType?</code > 或 <code >FooType!</code > 的 <code >foo</code >,尽量不要强行展开它以得到基本类型 (<code >foo!</code >)。</summary >
140
+ <details ><summary >如果你有个 <code >FooType?</code > 或 <code >FooType!</code > 的 <code >foo</code >,尽量不要强行展开它 (<code >foo!</code >)以得到它的关联值 。</summary >
139
141
140
142
If you have an identifier ` foo ` of type ` FooType? ` or ` FooType! ` , don't force-unwrap it to get to the underlying value (` foo! ` ) if possible.
141
143
</details >
@@ -165,22 +167,22 @@ foo?.callSomethingIfFooIsNotNil()
165
167
_ Rationale:_ Explicit ` if let ` -binding of optionals results in safer code. Force unwrapping is more prone to lead to runtime crashes.
166
168
</details >
167
169
168
- <h4 ><details ><summary >避免毫无保留地展开可选类型 </summary >Avoid Using Implicitly Unwrapped Optionals</details ></h4 >
170
+ <h2 ><details ><summary >避免隐式解析的可选类型 </summary >Avoid Using Implicitly Unwrapped Optionals</details ></h2 >
169
171
170
- <details ><summary >如果 foo 可能为 nil ,尽可能的用 <code >let foo: FooType?</code > 代替 <code >let foo: FooType!</code >(注意:一般情况下,<code >?</code >可以代替<code >!</code >)</summary >
172
+ <details ><summary >如果 foo 可能为 < code > nil</ code > ,尽可能的用 <code >let foo: FooType?</code > 代替 <code >let foo: FooType!</code >(注意:一般情况下,<code >?</code > 可以代替 <code >!</code >)</summary >
171
173
172
174
Where possible, use ` let foo: FooType? ` instead of ` let foo: FooType! ` if ` foo ` may be nil (Note that in general, ` ? ` can be used instead of ` ! ` ).
173
175
</details >
174
176
175
- <details ><summary ><i >理由:</i > 明确的可选类型产生了更安全的代码。无保留地展开可选类型也会挂 。</summary >
177
+ <details ><summary ><i >理由:</i > 明确的可选类型产生了更安全的代码。隐式解析的可选类型也可能会挂 。</summary >
176
178
177
179
_ Rationale:_ Explicit optionals result in safer code. Implicitly unwrapped optionals have the potential of crashing at runtime.
178
180
</details >
179
181
180
- <h4 ><details ><summary >对于只读属性和 <code >subscript</code >,选用隐式的 getters 方法</summary >
182
+ <h2 ><details ><summary >对于只读属性和 <code >subscript</code >,选用隐式的 getters 方法</summary >
181
183
182
184
Prefer implicit getters on read-only properties and subscripts
183
- </details ></h4 >
185
+ </details ></h2 >
184
186
185
187
<details ><summary >如果可以,省略只读属性和 <code >subscript</code > 的 <code >get</code > 关键字</summary >
186
188
@@ -220,7 +222,7 @@ subscript(index: Int) -> T {
220
222
_ Rationale:_ The intent and meaning of the first version is clear, and results in less code.
221
223
</details >
222
224
223
- <h4 ><details ><summary >对于顶级定义,永远明确的列出权限控制</summary >Always specify access control explicitly for top-level definitions</details ></h4 >
225
+ <h2 ><details ><summary >对于顶级定义,永远明确的列出权限控制</summary >Always specify access control explicitly for top-level definitions</details ></h2 >
224
226
225
227
<details ><summary >顶级函数,类型和变量,永远应该有着详尽的权限控制说明符</summary >
226
228
@@ -233,7 +235,7 @@ internal struct TheFez {}
233
235
private func doTheThings (things : [Thing]) {}
234
236
```
235
237
236
- <details ><summary >当然,这样也是恰当的,因为用了隐式权限控制 </summary >However, definitions within those can leave access control implicit, where appropriate:
238
+ <details ><summary >然而在这些函数/类型的内部,可以在合适的地方使用隐式权限控制: </summary >However, definitions within those can leave access control implicit, where appropriate:
237
239
</details >
238
240
239
241
``` swift
@@ -242,15 +244,15 @@ internal struct TheFez {
242
244
}
243
245
```
244
246
245
- <details ><summary ><i >理由:</i > 顶级定义指定为 <code >internal</code > 很少有恰当的,要明确的确保经过了仔细的判断。有了一个定义,重用同样的权限控制说明符就显得重复,所以默认的通常是合理的 。</summary >
247
+ <details ><summary ><i >理由:</i > 顶级定义指定为 <code >internal</code > 很少有恰当的,要明确的确保经过了仔细的判断。在定义的内部重用同样的权限控制说明符就显得重复,而且默认的通常是合理的 。</summary >
246
248
247
249
_ Rationale:_ It's rarely appropriate for top-level definitions to be specifically ` internal ` , and being explicit ensures that careful thought goes into that decision. Within a definition, reusing the same access control specifier is just duplicative, and the default is usually reasonable.
248
250
</details >
249
251
250
- <h4 ><details ><summary >当指定一个类型时,把 冒号和标识符 连在一起</summary >
252
+ <h2 ><details ><summary >当指定一个类型时,把 冒号和标识符 连在一起</summary >
251
253
252
254
When specifying a type, always associate the colon with the identifier
253
- </details ></h4 >
255
+ </details ></h2 >
254
256
255
257
<details ><summary >当指定标示符的类型时,冒号要紧跟着标示符,然后空一格再写类型</summary >
256
258
@@ -274,24 +276,26 @@ _Rationale:_ The type specifier is saying something about the _identifier_ so
274
276
it should be positioned with it.
275
277
</details >
276
278
279
+ <br />
280
+
277
281
<details ><summary >此外,指定字典类型时,键类型后紧跟着冒号,接着加一个空格,之后才是值类型。</summary >
278
282
279
283
Also, when specifying the type of a dictionary, always put the colon immediately after the key type, followed by a space and then the value type.
280
284
</details >
281
285
282
286
``` swift
283
- let capitals: [Country: City] = [ Sweden: Stockholm ]
287
+ let capitals: [Country: City] = [Sweden: Stockholm]
284
288
```
285
289
286
- <h4 ><details ><summary >需要时才写上 <code >self</code ></summary >
290
+ <h2 ><details ><summary >需要时才写上 <code >self</code ></summary >
287
291
288
292
Only explicitly refer to ` self ` when required
289
- </details ></h4 >
293
+ </details ></h2 >
290
294
291
295
292
296
293
297
294
- <details ><summary >当调用 <code >self</code > 的属性或方法时,<code >self</code > 用默认的隐式引用 :</summary >
298
+ <details ><summary >当调用 <code >self</code > 的属性或方法时,默认隐式引用 <code >self</code >:</summary >
295
299
296
300
When accessing properties or methods on ` self ` , leave the reference to ` self ` implicit by default:
297
301
</details >
@@ -325,15 +329,15 @@ extension History {
325
329
}
326
330
```
327
331
328
- <details ><summary ><i >原因:</i > 在闭包里用 <code >self</code > 更加凸显它的语义,并且避免了别处的冗长 </summary >
332
+ <details ><summary ><i >原因:</i > 在闭包里用 <code >self</code > 更加凸显它捕获 < code >self</ code > 的语义,别处避免了冗长 </summary >
329
333
330
334
_ Rationale:_ This makes the capturing semantics of ` self ` stand out more in closures, and avoids verbosity elsewhere.
331
335
</details >
332
336
333
- <h4 ><details ><summary >首选 <code >struct</code > 而非 <code >class</code ></summary >
337
+ <h2 ><details ><summary >首选 <code >struct</code > 而非 <code >class</code ></summary >
334
338
335
339
Prefer structs over classes
336
- </details ></h4 >
340
+ </details ></h2 >
337
341
338
342
<details ><summary >除非你需要 <code >class</code > 才能提供的功能(比如 identity 或 <code >deinit</code >ializers),不然就用 <code >struct</code ></summary >
339
343
@@ -399,17 +403,17 @@ struct Car: Vehicle {
399
403
}
400
404
```
401
405
402
- <details ><summary ><i >理由:</i > 值的类型更简单,容易辨别,并且通过 <code >let</code > 关键字可猜测行为 。</summary >
406
+ <details ><summary ><i >理由:</i > 值类型更简单,容易分析,并且 <code >let</code > 关键字的行为符合预期 。</summary >
403
407
404
408
_ Rationale:_ Value types are simpler, easier to reason about, and behave as expected with the ` let ` keyword.
405
409
</details >
406
410
407
- <h4 ><details ><summary >默认 <code >class</code > 为 <code >final</code ></summary >
411
+ <h2 ><details ><summary >默认 <code >class</code > 为 <code >final</code ></summary >
408
412
409
413
Make classes ` final ` by default
410
- </details ></h4 >
414
+ </details ></h2 >
411
415
412
- <details ><summary ><code >class</code > 应该用 <code >final</code > 修饰,并且只有在继承的有效需求已被确定时候才能去使用子类。即便在这种情况(前面提到的使用继承的情况)下,根据同样的规则(<code >class</code > 应该用 final</code > 修饰的规则),类中的定义(属性和方法等)也要尽可能的用 <code >final</code > 来修饰
416
+ <details ><summary ><code >class</code > 应该用 <code >final</code > 修饰,并且只有在继承的有效需求已被确定时候才能去使用子类。即便在这种情况(前面提到的使用继承的情况)下,根据同样的规则(<code >class</code > 应该用 < code > final</code > 修饰的规则),类中的定义(属性和方法等)也要尽可能的用 <code >final</code > 来修饰
413
417
</summary >
414
418
415
419
Classes should start as ` final ` , and only be changed to allow subclassing if a valid need for inheritance has been identified. Even in that case, as many definitions as possible _ within_ the class should be ` final ` as well, following the same rules.
@@ -420,12 +424,12 @@ Classes should start as `final`, and only be changed to allow subclassing if a v
420
424
_ Rationale:_ Composition is usually preferable to inheritance, and opting _ in_ to inheritance hopefully means that more thought will be put into the decision.
421
425
</details >
422
426
423
- <h4 ><details ><summary >能不写类型参数的就别写了</summary >
427
+ <h2 ><details ><summary >能不写类型参数的就别写了</summary >
424
428
425
429
Omit type parameters where possible
426
- </details ></h4 >
430
+ </details ></h2 >
427
431
428
- <details ><summary >参数化类型的方法可以省略接收者的类型参数,当他们对接收者来说一样时 。比如:</summary >
432
+ <details ><summary >当对接收者来说一样时,参数化类型的方法可以省略接收者的类型参数 。比如:</summary >
429
433
430
434
Methods of parameterized types can omit type parameters on the receiving type when they’re identical to the receiver’s. For example:
431
435
</details >
@@ -455,10 +459,10 @@ struct Composite<T> {
455
459
_ Rationale:_ Omitting redundant type parameters clarifies the intent, and makes it obvious by contrast when the returned type takes different type parameters.
456
460
</details >
457
461
458
- <h4 ><details ><summary >定义操作符 两边留空格</summary >
462
+ <h2 ><details ><summary >定义操作符 两边留空格</summary >
459
463
460
464
Use whitespace around operator definitions
461
- </details ></h4 >
465
+ </details ></h2 >
462
466
463
467
<details ><summary >当定义操作符 时,两边留空格。不要酱紫:</summary >
464
468
0 commit comments