19
19
* [ 闭包] ( #闭包 )
20
20
* [ 类型] ( #类型 )
21
21
* [ 常量] ( #常量 )
22
- * [ Optionals] ( #Optionals )
23
- * [ 类型推断] ( #类型推断 )
22
+ * [ Optional] ( #Optional )
23
+ * [ 类型推导] ( #类型推导 )
24
+ * [ 语法糖] ( #语法糖 )
24
25
* [ 控制流] ( #控制流 )
25
26
* [ Self的使用] ( #Self的使用 )
26
27
* [ 笑脸] ( #笑脸 )
@@ -237,15 +238,15 @@ class BoardLocation {
237
238
保持函数声明短小精悍,保持在一行内,花括号在同一行内开始:
238
239
239
240
``` swift
240
- func reticulateSplines (spline : Double [ ]) -> Bool {
241
+ func reticulateSplines (spline : [ Double ]) -> Bool {
241
242
// reticulate code goes here
242
243
}
243
244
```
244
245
245
246
对于有着长签名的函数,请在适当的位置进行断行且对后续行缩进一级:
246
247
247
248
``` swift
248
- func reticulateSplines (spline : Double [ ], adjustmentFactor : Double ,
249
+ func reticulateSplines (spline : [ Double ], adjustmentFactor : Double ,
249
250
translateConstant : Int , comment : String ) -> Bool {
250
251
// reticulate code goes here
251
252
}
@@ -293,7 +294,7 @@ let widthString: NSString = width.stringValue //NSString
293
294
294
295
** Tip:** 有一个方法可以帮你满足该项规则,将所有值都定义成常量,然后编译器提示的时候将其改为变量。
295
296
296
- ### Optionals
297
+ ### Optional
297
298
298
299
在nil值可能出现的情况下,将变量跟函数返回值的类型通过` ? ` 定义成Optional。
299
300
@@ -333,14 +334,34 @@ var currentBounds: CGRect = computeViewBounds()
333
334
334
335
** 注意** :遵循这条准则意味着使用描述性强的名称比之前更为重要了。
335
336
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
+
336
357
## 控制流
337
358
338
359
对于` for ` 循环,优选` for-in ` 风格而不是` for-condition-increment ` 风格:
339
360
340
361
** 优选:**
341
362
``` swift
342
- for _ in 0 .. 5 {
343
- println (" Hello five times" )
363
+ for _ in 0 ..< 3 {
364
+ println (" Hello three times" )
344
365
}
345
366
346
367
for person in attendeeList {
@@ -350,8 +371,8 @@ for person in attendeeList {
350
371
351
372
** 不建议使用:**
352
373
``` 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" )
355
376
}
356
377
357
378
for var i = 0 ; i < attendeeList.count ; i++ {
0 commit comments