Skip to content

Commit feae818

Browse files
committed
Add and translate 'Syntactic sugar' section
Signed-off-by: mrahmiao <mrahmiao@gmail.com>
1 parent 3a025ef commit feae818

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

zh_style_guide.md

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
* [闭包](#闭包)
2020
* [类型](#类型)
2121
* [常量](#常量)
22-
* [Optionals](#Optionals)
23-
* [类型推断](#类型推断)
22+
* [Optional](#Optional)
23+
* [类型推导](#类型推导)
24+
* [语法糖](#语法糖)
2425
* [控制流](#控制流)
2526
* [Self的使用](#Self的使用)
2627
* [笑脸](#笑脸)
@@ -237,15 +238,15 @@ class BoardLocation {
237238
保持函数声明短小精悍,保持在一行内,花括号在同一行内开始:
238239

239240
```swift
240-
func reticulateSplines(spline: Double[]) -> Bool {
241+
func reticulateSplines(spline: [Double]) -> Bool {
241242
// reticulate code goes here
242243
}
243244
```
244245

245246
对于有着长签名的函数,请在适当的位置进行断行且对后续行缩进一级:
246247

247248
```swift
248-
func reticulateSplines(spline: Double[], adjustmentFactor: Double,
249+
func reticulateSplines(spline: [Double], adjustmentFactor: Double,
249250
translateConstant: Int, comment: String) -> Bool {
250251
// reticulate code goes here
251252
}
@@ -293,7 +294,7 @@ let widthString: NSString = width.stringValue //NSString
293294

294295
**Tip:**有一个方法可以帮你满足该项规则,将所有值都定义成常量,然后编译器提示的时候将其改为变量。
295296

296-
### Optionals
297+
### Optional
297298

298299
在nil值可能出现的情况下,将变量跟函数返回值的类型通过`?`定义成Optional。
299300

@@ -333,14 +334,34 @@ var currentBounds: CGRect = computeViewBounds()
333334

334335
**注意**:遵循这条准则意味着使用描述性强的名称比之前更为重要了。
335336

337+
### 语法糖
338+
339+
Prefer the shortcut versions of type declarations over the full generics syntax.
340+
341+
使用简写的类型声明,而不是它的全泛型版本。
342+
343+
**优选:**
344+
```swift
345+
var deviceModels: [String]
346+
var employees: [Int: String]
347+
var faxNumber: Int?
348+
```
349+
350+
**不建议使用:**
351+
```swift
352+
var deviceModels: Array<String>
353+
var employees: Dictionary<Int, String>
354+
var faxNumber: Optional<Int>
355+
```
356+
336357
## 控制流
337358

338359
对于`for`循环,优选`for-in`风格而不是`for-condition-increment`风格:
339360

340361
**优选:**
341362
```swift
342-
for _ in 0..5 {
343-
println("Hello five times")
363+
for _ in 0..<3 {
364+
println("Hello three times")
344365
}
345366

346367
for person in attendeeList {
@@ -350,8 +371,8 @@ for person in attendeeList {
350371

351372
**不建议使用:**
352373
```swift
353-
for var i = 0; i < 5; i++ {
354-
println("Hello five times")
374+
for var i = 0; i < 3; i++ {
375+
println("Hello three times")
355376
}
356377

357378
for var i = 0; i < attendeeList.count; i++ {

0 commit comments

Comments
 (0)