@@ -57,7 +57,7 @@ func backward(_ s1: String, _ s2: String) -> Bool {
57
57
return s1 > s2
58
58
}
59
59
var reversedNames = names.sorted (by : backward)
60
- // reversedNames is equal to ["Ewa", "Daniella", "Chris", "Barry", "Alex"]
60
+ // reversedNames 等于 ["Ewa", "Daniella", "Chris", "Barry", "Alex"]
61
61
```
62
62
63
63
<!--
@@ -230,19 +230,19 @@ reversedNames = names.sorted(by: >)
230
230
231
231
``` swift
232
232
func someFunctionThatTakesAClosure (closure : () -> Void ) {
233
- // function body goes here
233
+ // 函数主体在这里
234
234
}
235
235
236
- // Here's how you call this function without using a trailing closure:
236
+ // 以下是如何在不使用尾随闭包的情况下调用此函数的示例:
237
237
238
238
someFunctionThatTakesAClosure (closure : {
239
- // closure's body goes here
239
+ // 闭包的主体在这里
240
240
})
241
241
242
- // Here's how you call this function with a trailing closure instead:
242
+ // 以下是如何使用尾随闭包调用此函数的示例:
243
243
244
244
someFunctionThatTakesAClosure () {
245
- // trailing closure's body goes here
245
+ // 尾随闭包的主体在这里
246
246
}
247
247
```
248
248
@@ -339,8 +339,8 @@ let strings = numbers.map { (number) -> String in
339
339
} while number > 0
340
340
return output
341
341
}
342
- // strings is inferred to be of type [String]
343
- // its value is ["OneSix", "FiveEight", "FiveOneZero"]
342
+ // strings 的类型被推断为 [String]
343
+ // 它的值是 ["OneSix", "FiveEight", "FiveOneZero"]
344
344
```
345
345
346
346
<!--
@@ -529,11 +529,11 @@ let incrementByTen = makeIncrementer(forIncrement: 10)
529
529
530
530
``` swift
531
531
incrementByTen ()
532
- // returns a value of 10
532
+ // 返回值为 10
533
533
incrementByTen ()
534
- // returns a value of 20
534
+ // 返回值为 20
535
535
incrementByTen ()
536
- // returns a value of 30
536
+ // 返回值为 30
537
537
```
538
538
539
539
<!--
@@ -565,7 +565,7 @@ incrementByTen()
565
565
``` swift
566
566
let incrementBySeven = makeIncrementer (forIncrement : 7 )
567
567
incrementBySeven ()
568
- // returns a value of 7
568
+ // 返回值为 7
569
569
```
570
570
571
571
<!--
@@ -584,7 +584,7 @@ incrementBySeven()
584
584
585
585
``` swift
586
586
incrementByTen ()
587
- // returns a value of 40
587
+ // 返回值为 40
588
588
```
589
589
--------
590
590
<!--
@@ -611,10 +611,10 @@ incrementByTen()
611
611
``` swift
612
612
let alsoIncrementByTen = incrementByTen
613
613
alsoIncrementByTen ()
614
- // returns a value of 50
614
+ // 返回值为 50
615
615
616
616
incrementByTen ()
617
- // returns a value of 60
617
+ // 返回值为 60
618
618
```
619
619
620
620
<!--
@@ -684,11 +684,11 @@ class SomeClass {
684
684
let instance = SomeClass ()
685
685
instance.doSomething ()
686
686
print (instance.x )
687
- // Prints " 200"
687
+ // 打印 “ 200”
688
688
689
689
completionHandlers.first ? ()
690
690
print (instance.x )
691
- // Prints " 100"
691
+ // 打印 “ 100”
692
692
```
693
693
694
694
<!--
@@ -844,16 +844,16 @@ struct SomeStruct {
844
844
``` swift
845
845
var customersInLine = [" Chris" , " Alex" , " Ewa" , " Barry" , " Daniella" ]
846
846
print (customersInLine.count )
847
- // Prints "5"
847
+ // 打印 “5”
848
848
849
849
let customerProvider = { customersInLine.remove (at : 0 ) }
850
850
print (customersInLine.count )
851
- // Prints "5"
851
+ // 打印 ”5“
852
852
853
853
print (" Now serving \( customerProvider ()) !" )
854
- // Prints " Now serving Chris!"
854
+ // 打印 “ Now serving Chris!”
855
855
print (customersInLine.count )
856
- // Prints "4"
856
+ // 打印 “4”
857
857
```
858
858
859
859
<!--
@@ -894,12 +894,12 @@ print(customersInLine.count)
894
894
将闭包作为参数传递给函数时,你能获得同样的延时计算行为。
895
895
896
896
``` swift
897
- // customersInLine is ["Alex", "Ewa", "Barry", "Daniella"]
897
+ // customersInLine 是 ["Alex", "Ewa", "Barry", "Daniella"]
898
898
func serve (customer customerProvider : () -> String ) {
899
899
print (" Now serving \( customerProvider ()) !" )
900
900
}
901
901
serve (customer : { customersInLine.remove (at : 0 ) } )
902
- // Prints " Now serving Alex!"
902
+ // 打印 “ Now serving Alex!”
903
903
```
904
904
905
905
<!--
@@ -920,12 +920,12 @@ serve(customer: { customersInLine.remove(at: 0) } )
920
920
上面列表中的 ` serve(customer:) ` 函数接受一个显式的闭包,该闭包返回顾客的名字。下面的 ` serve(customer:) ` 版本执行相同的操作,但它不接受显式的闭包,而是通过将其参数类型标记为 ` @autoclosure ` 特性来接受一个自动闭包。现在你可以调用这个函数,就像它接受一个 ` String ` 参数而不是闭包一样。因为 customerProvider 参数的类型被标记为 ` @autoclosure ` 特性,所以参数会自动转换为闭包。
921
921
922
922
``` swift
923
- // customersInLine is ["Ewa", "Barry", "Daniella"]
923
+ // customersInLine 是 ["Ewa", "Barry", "Daniella"]
924
924
func serve (customer customerProvider : @autoclosure () -> String ) {
925
925
print (" Now serving \( customerProvider ()) !" )
926
926
}
927
927
serve (customer : customersInLine.remove (at : 0 ))
928
- // Prints " Now serving Ewa!"
928
+ // 打印 “ Now serving Ewa!”
929
929
```
930
930
931
931
<!--
@@ -948,7 +948,7 @@ serve(customer: customersInLine.remove(at: 0))
948
948
如果您想要允许一个自动闭包可以逃逸,请同时使用 ` @autoclosure ` 和 ` @escaping ` 属性。` @escaping ` 属性在上面的 < doc:Closures#Escaping-Closures > 中进行了描述。
949
949
950
950
``` swift
951
- // customersInLine is ["Barry", "Daniella"]
951
+ // customersInLine 是 ["Barry", "Daniella"]
952
952
var customerProviders: [() -> String ] = []
953
953
func collectCustomerProviders (_ customerProvider : @autoclosure @escaping () -> String ) {
954
954
customerProviders.append (customerProvider)
@@ -957,12 +957,12 @@ collectCustomerProviders(customersInLine.remove(at: 0))
957
957
collectCustomerProviders (customersInLine.remove (at : 0 ))
958
958
959
959
print (" Collected \( customerProviders.count ) closures." )
960
- // Prints " Collected 2 closures."
960
+ // 打印 “ Collected 2 closures.”
961
961
for customerProvider in customerProviders {
962
962
print (" Now serving \( customerProvider ()) !" )
963
963
}
964
- // Prints " Now serving Barry!"
965
- // Prints " Now serving Daniella!"
964
+ // 打印 “ Now serving Barry!”
965
+ // 打印 ” Now serving Daniella!“
966
966
```
967
967
968
968
<!--
0 commit comments