Room { get { return rooms[i] } set { rooms[i] = newValue } } func printNumberOfRooms() { print("The number of rooms is \(numberOfRooms)") } var address: Address? } class Room { let name: String init(name: String) { self.name = name } } class Address { var buildingName: String? var build"> Room { get { return rooms[i] } set { rooms[i] = newValue } } func printNumberOfRooms() { print("The number of rooms is \(numberOfRooms)") } var address: Address? } class Room { let name: String init(name: String) { self.name = name } } class Address { var buildingName: String? var build"> Room { get { return rooms[i] } set { rooms[i] = newValue } } func printNumberOfRooms() { print("The number of rooms is \(numberOfRooms)") } var address: Address? } class Room { let name: String init(name: String) { self.name = name } } class Address { var buildingName: String? var build">
//class Person {
// var residence: Residence?
//}
//
//class Residence {
// var numberOfRooms = 1
//}
//
//let john = Person()
//
////let roomCount = john.residence!.numberOfRooms
//// 这会引发运行时错误
//
//if let roomCount = john.residence?.numberOfRooms {
// print("John's residence has \\(roomCount) room(s).")
//} else {
// print("Unable to retrieve the number of rooms.")
//}
//// 打印“Unable to retrieve the number of rooms.”
class Person {
var residence: Residence?
}
class Residence {
var rooms: [Room] = []
var numberOfRooms: Int {
return rooms.count
}
subscript(i: Int) -> Room {
get {
return rooms[i]
}
set {
rooms[i] = newValue
}
}
func printNumberOfRooms() {
print("The number of rooms is \\(numberOfRooms)")
}
var address: Address?
}
class Room {
let name: String
init(name: String) { self.name = name }
}
class Address {
var buildingName: String?
var buildingNumber: String?
var street: String?
func buildingIdentifier() -> String? {
if buildingName != nil {
return buildingName
} else if let buildingNumber = buildingNumber, let street = street {
return "\\(buildingNumber) \\(street)"
} else {
return nil
}
}
}
let john = Person()
if let roomCount = john.residence?.numberOfRooms {
print("John's residence has \\(roomCount) room(s).")
} else {
print("Unable to retrieve the number of rooms.")
}
// 打印“Unable to retrieve the number of rooms.”
let someAddress = Address()
someAddress.buildingNumber = "29"
someAddress.street = "Acacia Road"
john.residence?.address = someAddress
func createAddress() -> Address {
print("Function was called.")
let someAddress = Address()
someAddress.buildingNumber = "29"
someAddress.street = "Acacia Road"
return someAddress
}
john.residence?.address = createAddress()
if john.residence?.printNumberOfRooms() != nil {
print("It was possible to print the number of rooms.")
} else {
print("It was not possible to print the number of rooms.")
}
// 打印“It was not possible to print the number of rooms.”
if (john.residence?.address = someAddress) != nil {
print("It was possible to set the address.")
} else {
print("It was not possible to set the address.")
}
// 打印“It was not possible to set the address.”
if let firstRoomName = john.residence?[0].name {
print("The first room name is \\(firstRoomName).")
} else {
print("Unable to retrieve the first room name.")
}
// 打印“Unable to retrieve the first room name.”
john.residence?[0] = Room(name: "Bathroom")
if let firstRoomName2 = john.residence?[0].name {
print("The first room name is \\(firstRoomName2).")
} else {
print("Unable to retrieve the first room name.")
}
let johnsHouse = Residence()
johnsHouse.rooms.append(Room(name: "Living Room"))
johnsHouse.rooms.append(Room(name: "Kitchen"))
john.residence = johnsHouse
if let firstRoomName = john.residence?[0].name {
print("The first room name is \\(firstRoomName).")
} else {
print("Unable to retrieve the first room name.")
}
// 打印“The first room name is Living Room.”
var testScores = ["Dave": [86, 82, 84], "Bev": [79, 94, 81]]
testScores["Dave"]?[0] = 91
testScores["Bev"]?[0] += 1
testScores["Brian"]?[0] = 72
// "Dave" 数组现在是 [91, 82, 84],"Bev" 数组现在是 [80, 94, 81]
if let johnsStreet = john.residence?.address?.street {
print("John's street name is \\(johnsStreet).")
} else {
print("Unable to retrieve the address.")
}
// 打印“Unable to retrieve the address.”
let johnsAddress = Address()
johnsAddress.buildingName = "The Larches"
johnsAddress.street = "Laurel Street"
john.residence?.address = johnsAddress
if let johnsStreet = john.residence?.address?.street {
print("John's street name is \\(johnsStreet).")
} else {
print("Unable to retrieve the address.")
}
// 打印“John's street name is Laurel Street.”
if let buildingIdentifier = john.residence?.address?.buildingIdentifier() {
print("John's building identifier is \\(buildingIdentifier).")
}
// 打印“John's building identifier is The Larches.”
if let beginsWithThe =
john.residence?.address?.buildingIdentifier()?.hasPrefix("The") {
if beginsWithThe {
print("John's building identifier begins with \\"The\\".")
} else {
print("John's building identifier does not begin with \\"The\\".")
}
}
// 打印“John's building identifier begins with "The".”
<aside>
🌼 通过可选链访问下标时,?
要加在可选项的后面,而不是下标的后面,如下:
john.residence?[0] = Room(name: "Bathroom")
</aside>
<aside>
🦄 如果通过下标访问的值是一个可选类型,则继续访问它的下标时,需要在其后面加 ?
,也就是说 ?
要加载一个可选类型的后面。如通过字典键访问到的值是一个数组,继续访问数组中的元素。
</aside>
var testScores = ["Dave": [86, 82, 84], "Bev": [79, 94, 81]]
testScores["Dave"]?[0] = 91
testScores["Bev"]?[0] += 1
testScores["Brian"]?[0] = 72
// "Dave" 数组现在是 [91, 82, 84],"Bev" 数组现在是 [80, 94, 81]