Skip to content

Commit 306960c

Browse files
committed
Merge branch '1329-languageguide--optional-chainingmd' of github.com:MOXCOOT/the-swift-programming-language-in-chinese into 1329-languageguide--optional-chainingmd
2 parents d0bca48 + fcf843f commit 306960c

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 可选链式调用(Optional Chaining)
22

3-
无需解包即可访问可选值的成员
3+
在不解包的情况下访问可选值的成员
44

55
可选链式调用(Optional Chaining)是一种可以在当前值可能为 `nil` 的可选值上请求和调用属性、方法及下标的方法。如果可选值有值,那么调用就会成功;如果可选值是 `nil`,那么调用将返回 `nil`。多个调用可以连接在一起形成一个调用链,如果其中任何一个节点为 `nil`,整个调用链都会失败,即返回 `nil`
66

@@ -75,9 +75,9 @@ john.residence = Residence()
7575

7676
```swift
7777
if let roomCount = john.residence?.numberOfRooms {
78-
print("John's residence has \(roomCount) room(s).")
78+
print("John's residence has \(roomCount) room(s).")
7979
} else {
80-
print("Unable to retrieve the number of rooms.")
80+
print("Unable to retrieve the number of rooms.")
8181
}
8282
// 打印 “John's residence has 1 room(s).”
8383
```
@@ -102,7 +102,7 @@ class Person {
102102

103103
```swift
104104
class Residence {
105-
var rooms = [Room]()
105+
var rooms: [Room] = []
106106
var numberOfRooms: Int {
107107
return rooms.count
108108
}
@@ -146,10 +146,10 @@ class Address {
146146
var buildingNumber: String?
147147
var street: String?
148148
func buildingIdentifier() -> String? {
149-
if buildingName != nil {
150-
return buildingName
151-
} else if buildingNumber != nil && street != nil {
149+
if let buildingNumber = buildingNumber, let street = street {
152150
return "\(buildingNumber) \(street)"
151+
} else if buildingName != nil {
152+
return buildingName
153153
} else {
154154
return nil
155155
}
@@ -238,9 +238,9 @@ if john.residence?.printNumberOfRooms() != nil {
238238

239239
```swift
240240
if (john.residence?.address = someAddress) != nil {
241-
print("It was possible to set the address.")
241+
print("It was possible to set the address.")
242242
} else {
243-
print("It was not possible to set the address.")
243+
print("It was not possible to set the address.")
244244
}
245245
// 打印 “It was not possible to set the address.”
246246
```
@@ -284,9 +284,9 @@ johnsHouse.rooms.append(Room(name: "Kitchen"))
284284
john.residence = johnsHouse
285285

286286
if let firstRoomName = john.residence?[0].name {
287-
print("The first room name is \(firstRoomName).")
287+
print("The first room name is \(firstRoomName).")
288288
} else {
289-
print("Unable to retrieve the first room name.")
289+
print("Unable to retrieve the first room name.")
290290
}
291291
// 打印 “The first room name is Living Room.”
292292
```
@@ -345,7 +345,7 @@ let johnsAddress = Address()
345345
johnsAddress.buildingName = "The Larches"
346346
johnsAddress.street = "Laurel Street"
347347
john.residence?.address = johnsAddress
348-
348+
349349
if let johnsStreet = john.residence?.address?.street {
350350
print("John's street name is \(johnsStreet).")
351351
} else {
@@ -375,12 +375,12 @@ if let buildingIdentifier = john.residence?.address?.buildingIdentifier() {
375375

376376
```swift
377377
if let beginsWithThe =
378-
john.residence?.address?.buildingIdentifier()?.hasPrefix("The") {
379-
if beginsWithThe {
380-
print("John's building identifier begins with \"The\".")
381-
} else {
382-
print("John's building identifier does not begin with \"The\".")
383-
}
378+
john.residence?.address?.buildingIdentifier()?.hasPrefix("The") {
379+
if beginsWithThe {
380+
print("John's building identifier begins with \"The\".")
381+
} else {
382+
print("John's building identifier doesn't begin with \"The\".")
383+
}
384384
}
385385
// 打印 “John's building identifier begins with "The".”
386386
```

0 commit comments

Comments
 (0)