Skip to content

Commit 471e26e

Browse files
authored
Merge pull request #1399 from wang-jiaolong/1309-languageguide-closures-append
1309-languageguide-closures-append
2 parents 20dd824 + 172cda2 commit 471e26e

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

swift-6-beta.docc/LanguageGuide/Closures.md

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func backward(_ s1: String, _ s2: String) -> Bool {
5757
return s1 > s2
5858
}
5959
var reversedNames = names.sorted(by: backward)
60-
// reversedNames is equal to ["Ewa", "Daniella", "Chris", "Barry", "Alex"]
60+
// reversedNames 等于 ["Ewa", "Daniella", "Chris", "Barry", "Alex"]
6161
```
6262

6363
<!--
@@ -230,19 +230,19 @@ reversedNames = names.sorted(by: >)
230230

231231
```swift
232232
func someFunctionThatTakesAClosure(closure: () -> Void) {
233-
// function body goes here
233+
// 函数主体在这里
234234
}
235235

236-
// Here's how you call this function without using a trailing closure:
236+
// 以下是如何在不使用尾随闭包的情况下调用此函数的示例:
237237

238238
someFunctionThatTakesAClosure(closure: {
239-
// closure's body goes here
239+
// 闭包的主体在这里
240240
})
241241

242-
// Here's how you call this function with a trailing closure instead:
242+
// 以下是如何使用尾随闭包调用此函数的示例:
243243

244244
someFunctionThatTakesAClosure() {
245-
// trailing closure's body goes here
245+
// 尾随闭包的主体在这里
246246
}
247247
```
248248

@@ -339,8 +339,8 @@ let strings = numbers.map { (number) -> String in
339339
} while number > 0
340340
return output
341341
}
342-
// strings is inferred to be of type [String]
343-
// its value is ["OneSix", "FiveEight", "FiveOneZero"]
342+
// strings 的类型被推断为 [String]
343+
// 它的值是 ["OneSix", "FiveEight", "FiveOneZero"]
344344
```
345345

346346
<!--
@@ -529,11 +529,11 @@ let incrementByTen = makeIncrementer(forIncrement: 10)
529529

530530
```swift
531531
incrementByTen()
532-
// returns a value of 10
532+
// 返回值为 10
533533
incrementByTen()
534-
// returns a value of 20
534+
// 返回值为 20
535535
incrementByTen()
536-
// returns a value of 30
536+
// 返回值为 30
537537
```
538538

539539
<!--
@@ -565,7 +565,7 @@ incrementByTen()
565565
```swift
566566
let incrementBySeven = makeIncrementer(forIncrement: 7)
567567
incrementBySeven()
568-
// returns a value of 7
568+
// 返回值为 7
569569
```
570570

571571
<!--
@@ -584,7 +584,7 @@ incrementBySeven()
584584

585585
```swift
586586
incrementByTen()
587-
// returns a value of 40
587+
// 返回值为 40
588588
```
589589
--------
590590
<!--
@@ -611,10 +611,10 @@ incrementByTen()
611611
```swift
612612
let alsoIncrementByTen = incrementByTen
613613
alsoIncrementByTen()
614-
// returns a value of 50
614+
// 返回值为 50
615615

616616
incrementByTen()
617-
// returns a value of 60
617+
// 返回值为 60
618618
```
619619

620620
<!--
@@ -684,11 +684,11 @@ class SomeClass {
684684
let instance = SomeClass()
685685
instance.doSomething()
686686
print(instance.x)
687-
// Prints "200"
687+
// 打印 “200
688688

689689
completionHandlers.first?()
690690
print(instance.x)
691-
// Prints "100"
691+
// 打印 “100
692692
```
693693

694694
<!--
@@ -844,16 +844,16 @@ struct SomeStruct {
844844
```swift
845845
var customersInLine = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
846846
print(customersInLine.count)
847-
// Prints "5"
847+
// 打印 “5”
848848

849849
let customerProvider = { customersInLine.remove(at: 0) }
850850
print(customersInLine.count)
851-
// Prints "5"
851+
// 打印 ”5“
852852

853853
print("Now serving \(customerProvider())!")
854-
// Prints "Now serving Chris!"
854+
// 打印 “Now serving Chris!
855855
print(customersInLine.count)
856-
// Prints "4"
856+
// 打印 “4”
857857
```
858858

859859
<!--
@@ -894,12 +894,12 @@ print(customersInLine.count)
894894
将闭包作为参数传递给函数时,你能获得同样的延时计算行为。
895895

896896
```swift
897-
// customersInLine is ["Alex", "Ewa", "Barry", "Daniella"]
897+
// customersInLine ["Alex", "Ewa", "Barry", "Daniella"]
898898
func serve(customer customerProvider: () -> String) {
899899
print("Now serving \(customerProvider())!")
900900
}
901901
serve(customer: { customersInLine.remove(at: 0) } )
902-
// Prints "Now serving Alex!"
902+
// 打印 “Now serving Alex!
903903
```
904904

905905
<!--
@@ -920,12 +920,12 @@ serve(customer: { customersInLine.remove(at: 0) } )
920920
上面列表中的 `serve(customer:)` 函数接受一个显式的闭包,该闭包返回顾客的名字。下面的 `serve(customer:)` 版本执行相同的操作,但它不接受显式的闭包,而是通过将其参数类型标记为 `@autoclosure` 特性来接受一个自动闭包。现在你可以调用这个函数,就像它接受一个 `String` 参数而不是闭包一样。因为 customerProvider 参数的类型被标记为 `@autoclosure` 特性,所以参数会自动转换为闭包。
921921

922922
```swift
923-
// customersInLine is ["Ewa", "Barry", "Daniella"]
923+
// customersInLine ["Ewa", "Barry", "Daniella"]
924924
func serve(customer customerProvider: @autoclosure () -> String) {
925925
print("Now serving \(customerProvider())!")
926926
}
927927
serve(customer: customersInLine.remove(at: 0))
928-
// Prints "Now serving Ewa!"
928+
// 打印 “Now serving Ewa!
929929
```
930930

931931
<!--
@@ -948,7 +948,7 @@ serve(customer: customersInLine.remove(at: 0))
948948
如果您想要允许一个自动闭包可以逃逸,请同时使用 `@autoclosure``@escaping` 属性。`@escaping` 属性在上面的 <doc:Closures#Escaping-Closures> 中进行了描述。
949949

950950
```swift
951-
// customersInLine is ["Barry", "Daniella"]
951+
// customersInLine ["Barry", "Daniella"]
952952
var customerProviders: [() -> String] = []
953953
func collectCustomerProviders(_ customerProvider: @autoclosure @escaping () -> String) {
954954
customerProviders.append(customerProvider)
@@ -957,12 +957,12 @@ collectCustomerProviders(customersInLine.remove(at: 0))
957957
collectCustomerProviders(customersInLine.remove(at: 0))
958958

959959
print("Collected \(customerProviders.count) closures.")
960-
// Prints "Collected 2 closures."
960+
// 打印 “Collected 2 closures.
961961
for customerProvider in customerProviders {
962962
print("Now serving \(customerProvider())!")
963963
}
964-
// Prints "Now serving Barry!"
965-
// Prints "Now serving Daniella!"
964+
// 打印 “Now serving Barry!
965+
// 打印 ”Now serving Daniella!
966966
```
967967

968968
<!--

0 commit comments

Comments
 (0)