1
1
# 可选链式调用(Optional Chaining)
2
2
3
- 无需解包即可访问可选值的成员 。
3
+ 在不解包的情况下访问可选值的成员 。
4
4
5
5
可选链式调用(Optional Chaining)是一种可以在当前值可能为 ` nil ` 的可选值上请求和调用属性、方法及下标的方法。如果可选值有值,那么调用就会成功;如果可选值是 ` nil ` ,那么调用将返回 ` nil ` 。多个调用可以连接在一起形成一个调用链,如果其中任何一个节点为 ` nil ` ,整个调用链都会失败,即返回 ` nil ` 。
6
6
@@ -75,9 +75,9 @@ john.residence = Residence()
75
75
76
76
``` swift
77
77
if let roomCount = john.residence? .numberOfRooms {
78
- print (" John's residence has \( roomCount ) room(s)." )
78
+ print (" John's residence has \( roomCount ) room(s)." )
79
79
} else {
80
- print (" Unable to retrieve the number of rooms." )
80
+ print (" Unable to retrieve the number of rooms." )
81
81
}
82
82
// 打印 “John's residence has 1 room(s).”
83
83
```
@@ -102,7 +102,7 @@ class Person {
102
102
103
103
``` swift
104
104
class Residence {
105
- var rooms = [Room]()
105
+ var rooms: [Room] = []
106
106
var numberOfRooms: Int {
107
107
return rooms.count
108
108
}
@@ -146,10 +146,10 @@ class Address {
146
146
var buildingNumber: String ?
147
147
var street: String ?
148
148
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 {
152
150
return " \( buildingNumber ) \( street ) "
151
+ } else if buildingName != nil {
152
+ return buildingName
153
153
} else {
154
154
return nil
155
155
}
@@ -238,9 +238,9 @@ if john.residence?.printNumberOfRooms() != nil {
238
238
239
239
``` swift
240
240
if (john.residence ? .address = someAddress) != nil {
241
- print (" It was possible to set the address." )
241
+ print (" It was possible to set the address." )
242
242
} else {
243
- print (" It was not possible to set the address." )
243
+ print (" It was not possible to set the address." )
244
244
}
245
245
// 打印 “It was not possible to set the address.”
246
246
```
@@ -284,9 +284,9 @@ johnsHouse.rooms.append(Room(name: "Kitchen"))
284
284
john.residence = johnsHouse
285
285
286
286
if let firstRoomName = john.residence? [0 ].name {
287
- print (" The first room name is \( firstRoomName ) ." )
287
+ print (" The first room name is \( firstRoomName ) ." )
288
288
} else {
289
- print (" Unable to retrieve the first room name." )
289
+ print (" Unable to retrieve the first room name." )
290
290
}
291
291
// 打印 “The first room name is Living Room.”
292
292
```
@@ -345,7 +345,7 @@ let johnsAddress = Address()
345
345
johnsAddress.buildingName = " The Larches"
346
346
johnsAddress.street = " Laurel Street"
347
347
john.residence ? .address = johnsAddress
348
-
348
+
349
349
if let johnsStreet = john.residence? .address? .street {
350
350
print (" John's street name is \( johnsStreet ) ." )
351
351
} else {
@@ -375,12 +375,12 @@ if let buildingIdentifier = john.residence?.address?.buildingIdentifier() {
375
375
376
376
``` swift
377
377
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
+ }
384
384
}
385
385
// 打印 “John's building identifier begins with "The".”
386
386
```
0 commit comments