8000 自认为更准确通顺的翻译 · PaPazhao/swift-style-guide@46fe8b0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 46fe8b0

Browse files
committed
自认为更准确通顺的翻译
1 parent ce4b978 commit 46fe8b0

File tree

1 file changed

+44
-40
lines changed

1 file changed

+44
-40
lines changed

README_CN.md

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
## [原版戳这里](https://github.com/github/swift-style-guide)
66

7-
哪里不对或者不准确的,若能指出(我把原文贴上来了,点击左侧三角形就能展开用来对照 2015-12-13)
8-
感激不尽~~~
7+
哪里不对或者不准确的,(我把原文贴上来了,点击左侧三角形就能展开用来对照 2015-12-13)
8+
若能指出,感激不尽~~~
99

1010
如果没能及时更新,可能比较忙,或者比较懒 →_
1111
翻译后可以 email 或 `pull request`
@@ -17,7 +17,7 @@
1717
A guide to our Swift style and conventions.
1818
</details></h1>
1919

20-
<details><summary>本文尝试做到以下几点(大概的先后顺序):</summary>
20+
<details><summary>按大概的先后顺序,本文尝试做到以下几点:</summary>
2121
This is an attempt to encourage patterns that accomplish the following goals (in
2222
rough priority order):
2323
</details>
@@ -37,22 +37,22 @@ then open a pull request. :zap:
3737

3838
----
3939

40-
<h4><details><summary>留空白</summary>Whitespace</details></h4>
40+
<h2><details><summary>留空白</summary>Whitespace</details></h2>
4141

4242
<ul>
43-
<li><details><summary>用 Tabs,而非 空格</summary>Tabs, not spaces.</details></li>
43+
<li><details><summary>用 tab,而非 空格</summary>Tabs, not spaces.</details></li>
4444
<li><details><summary>文件结束时留一空行</summary>End files with a newline.</details></li>
4545
<li><details><summary>用足够的空行把代码分割成合理的块</summary>Make liberal use of vertical whitespace to divide code into logical chunks.</details></li>
4646
<li><details><summary>不要在一行结尾留下空白</summary>Don’t leave trailing whitespace.</details>
4747
<ul><li><details><summary>千万别在空行留下缩进</summary>Not even leading indentation on blank lines.</details></li></ul>
4848
</li>
4949
</ul>
5050

51-
<h4><details>
51+
<h2><details>
5252
<summary>能用 <code>let</code> 尽量用 <code>let</code> 而不是 <code>var</code></summary>
5353

5454
Prefer `let`-bindings over `var`-bindings wherever possible
55-
</details></h4>
55+
</details></h2>
5656

5757
<details>
5858
<summary>
@@ -70,33 +70,35 @@ Use `let foo = …` over `var foo = …` wherever possible (and when in doubt).
7070
_Rationale:_ The intent and meaning of both keywords is clear, but *let-by-default* results in safer and clearer code.
7171
</details>
7272

73+
<br />
74+
7375
<details>
7476
<summary>
75-
<code>let</code>-有保障 并且它的值的永远不会变对程序猿也是个 <i>清晰的标记</i>,对于它的用法,之后的代码可以做个强而有力的推断。</summary>
77+
<code>let</code> 保障它的值的永远不会变,对程序猿也是个 <i>清晰的标记</i>。因此对于它的用法,之后的代码可以做个强而有力的推断。</summary>
7678

7779
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.
7880
</details>
7981

8082
<details>
8183
<summary>
82-
猜测代码更容易了。不然一旦你用了 <code>var</code>,还要去推测值会不会变,这时候你就不得不人肉去检查。
84+
理解代码也更容易了。不然一旦你用了 <code>var</code>,还要去推测值会不会变,这时候你就不得不人肉去检查。
8385
</summary>
8486

8587
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.
8688
</details>
8789

8890
<details>
8991
<summary>
90-
这样,无论何时你看到 <code>var</code>,就假设它会变,并问自己为啥。
92+
相应地,无论何时你看到 <code>var</code>,就假设它会变,并问自己为啥。
9193
</summary>
9294

9395
Accordingly, whenever you see a `var` identifier being used, assume that it will change and ask yourself why.
9496
</details>
9597

96-
<h4><details><summary>
98+
<h2><details><summary>
9799
尽早地 <code>return</code> 或者 <code>break</code></summary>
98100

99-
Return and break early</details></h4>
101+
Return and break early</details></h2>
100102

101103
<details>
102104
<summary>当你遇到某些操作需要通过条件判断去执行,应当尽早地退出判断条件:你不应该用下面这种写法</summary>
@@ -133,9 +135,9 @@ You can also do it with `if` statement, but using `guard` is prefered
133135
because `guard` statement without `return`, `break` or `continue` produces a compile-time error, so exit is guaranteed.
134136
</details>
135137

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>
137139

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>
139141

140142
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.
141143
</details>
@@ -165,22 +167,22 @@ foo?.callSomethingIfFooIsNotNil()
165167
_Rationale:_ Explicit `if let`-binding of optionals results in safer code. Force unwrapping is more prone to lead to runtime crashes.
166168
</details>
167169

168-
<h4><details><summary>避免毫无保留地展开可选类型</summary>Avoid Using Implicitly Unwrapped Optionals</details></h4>
170+
<h2><details><summary>避免隐式解析的可选类型</summary>Avoid Using Implicitly Unwrapped Optionals</details></h2>
169171

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>
171173

172174
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 `!`).
173175
</details>
174176

175-
<details><summary><i>理由:</i> 明确的可选类型产生了更安全的代码。无保留地展开可选类型也会挂。</summary>
177+
<details><summary><i>理由:</i> 明确的可选类型产生了更安全的代码。隐式解析的可选类型也可能会挂。</summary>
176178

177179
_Rationale:_ Explicit optionals result in safer code. Implicitly unwrapped optionals have the potential of crashing at runtime.
178180
</details>
179181

180-
<h4><details><summary>对于只读属性和 <code>subscript</code>,选用隐式的 getters 方法</summary>
182+
<h2><details><summary>对于只读属性和 <code>subscript</code>,选用隐式的 getters 方法</summary>
181183

182184
Prefer implicit getters on read-only properties and subscripts
183-
</details></h4>
185+
</details></h2>
184186

185187
<details><summary>如果可以,省略只读属性和 <code>subscript</code> 的 <code>get</code> 关键字</summary>
186188

@@ -220,7 +222,7 @@ subscript(index: Int) -> T {
220222
_Rationale:_ The intent and meaning of the first version is clear, and results in less code.
221223
</details>
222224

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>
224226

225227
<details><summary>顶级函数,类型和变量,永远应该有着详尽的权限控制说明符</summary>
226228

@@ -233,7 +235,7 @@ internal struct TheFez {}
233235
private func doTheThings(things: [Thing]) {}
234236
```
235237

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:
237239
</details>
238240

239241
```swift
@@ -242,15 +244,15 @@ internal struct TheFez {
242244
}
243245
```
244246

245-
<details><summary><i>理由:</i> 顶级定义指定为 <code>internal</code> 很少有恰当的,要明确的确保经过了仔细的判断。有了一个定义,重用同样的权限控制说明符就显得重复,所以默认的通常是合理的。</summary>
247+
<details><summary><i>理由:</i> 顶级定义指定为 <code>internal</code> 很少有恰当的,要明确的确保经过了仔细的判断。在定义的内部重用同样的权限控制说明符就显得重复,而且默认的通常是合理的。</summary>
246248

247249
_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.
248250
</details>
249251

250-
<h4><details><summary>当指定一个类型时,把 冒号和标识符 连在一起</summary>
252+
<h2><details><summary>当指定一个类型时,把 冒号和标识符 连在一起</summary>
251253

252254
When specifying a type, always associate the colon with the identifier
253-
</details></h4>
255+
</details></h2>
254256

255257
<details><summary>当指定标示符的类型时,冒号要紧跟着标示符,然后空一格再写类型</summary>
256258

@@ -274,24 +276,26 @@ _Rationale:_ The type specifier is saying something about the _identifier_ so
274276
it should be positioned with it.
275277
</details>
276278

279+
<br />
280+
277281
<details><summary>此外,指定字典类型时,键类型后紧跟着冒号,接着加一个空格,之后才是值类型。</summary>
278282

279283
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.
280284
</details>
281285

282286
```swift
283-
let capitals: [Country: City] = [ Sweden: Stockholm ]
287+
let capitals: [Country: City] = [Sweden: Stockholm]
284288
```
285289

286-
<h4><details><summary>需要时才写上 <code>self</code></summary>
290+
<h2><details><summary>需要时才写上 <code>self</code></summary>
287291

288292
Only explicitly refer to `self` when required
289-
</details></h4>
293+
</details></h2>
290294

291295

292296

293297

294-
<details><summary>当调用 <code>self</code> 的属性或方法时,<code>self</code> 用默认的隐式引用:</summary>
298+
<details><summary>当调用 <code>self</code> 的属性或方法时,默认隐式引用<code>self</code>:</summary>
295299

296300
When accessing properties or methods on `self`, leave the reference to `self` implicit by default:
297301
</details>
@@ -325,15 +329,15 @@ extension History {
325329
}
326330
```
327331

328-
<details><summary><i>原因:</i> 在闭包里用 <code>self</code> 更加凸显它的语义,并且避免了别处的冗长</summary>
332+
<details><summary><i>原因:</i> 在闭包里用 <code>self</code> 更加凸显它捕获 <code>self</code> 的语义,别处避免了冗长</summary>
329333

330334
_Rationale:_ This makes the capturing semantics of `self` stand out more in closures, and avoids verbosity elsewhere.
331335
</details>
332336

333-
<h4><details><summary>首选 <code>struct</code> 而非 <code>class</code></summary>
337+
<h2><details><summary>首选 <code>struct</code> 而非 <code>class</code></summary>
334338

335339
Prefer structs over classes
336-
</details></h4>
340+
</details></h2>
337341

338342
<details><summary>除非你需要 <code>class</code> 才能提供的功能(比如 identity 或 <code>deinit</code>ializers),不然就用 <code>struct</code></summary>
339343

@@ -399,17 +403,17 @@ struct Car: Vehicle {
399403
}
400404
```
401405

402-
<details><summary><i>理由:</i> 值的类型更简单,容易辨别,并且通过 <code>let</code> 关键字可猜测行为。</summary>
406+
<details><summary><i>理由:</i> 值类型更简单,容易分析,并且 <code>let</code> 关键字的行为符合预期。</summary>
403407

404408
_Rationale:_ Value types are simpler, easier to reason about, and behave as expected with the `let` keyword.
405409
</details>
406410

407-
<h4><details><summary>默认 <code>class</code> 为 <code>final</code></summary>
411+
<h2><details><summary>默认 <code>class</code> 为 <code>final</code></summary>
408412

409413
Make classes `final` by default
410-
</details></h4>
414+
</details></h2>
411415

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> 来修饰
413417
</summary>
414418

415419
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
420424
_Rationale:_ Composition is usually preferable to inheritance, and opting _in_ to inheritance hopefully means that more thought will be put into the decision.
421425
</details>
422426

423-
<h4><details><summary>能不写类型参数的就别写了</summary>
427+
<h2><details><summary>能不写类型参数的就别写了</summary>
424428

425429
Omit type parameters where possible
426-
</details></h4>
430+
</details></h2>
427431

428-
<details><summary>参数化类型的方法可以省略接收者的类型参数,当他们对接收者来说一样时。比如:</summary>
432+
<details><summary>当对接收者来说一样时,参数化类型的方法可以省略接收者的类型参数。比如:</summary>
429433

430434
Methods of parameterized types can omit type parameters on the receiving type when they’re identical to the receiver’s. For example:
431435
</details>
@@ -455,10 +459,10 @@ struct Composite<T> {
455459
_Rationale:_ Omitting redundant type parameters clarifies the intent, and makes it obvious by contrast when the returned type takes different type parameters.
456460
</details>
457461

458-
<h4><details><summary>定义操作符 两边留空格</summary>
462+
<h2><details><summary>定义操作符 两边留空格</summary>
459463

460464
Use whitespace around operator definitions
461-
</details></h4>
465+
</details></h2>
462466

463467
<details><summary>当定义操作符 时,两边留空格。不要酱紫:</summary>
464468

0 commit comments

Comments
 (0)
0