一種可以在當(dāng)前值可能為nil的可選值上請求和調(diào)用屬性魁衙、方法及下標(biāo)的方法
1 使用可選鏈?zhǔn)秸{(diào)用代替強(qiáng)制展開
可選鏈?zhǔn)秸{(diào)用的返回結(jié)果與原本的返回結(jié)果具有相同的類型蛋叼,但是被包裝成了一個(gè)可選值碉熄。
class Person {
var residence: Residence?
}
class Residence {
var numberOfRooms = 1
}
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.")
}
john.residence = Residence()
if let roomCount = john.residence?.numberOfRooms {
print("John's residence has \(roomCount) room(s).")
} else {
print("Unable to retrieve the number of rooms.")
}
2 為可選鏈?zhǔn)秸{(diào)用定義模型類
class Person2 {
var residence: Residence2?
}
class Residence2 {
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 buildingNumber != nil && street != nil {
return "\(buildingNumber) \(street)"
} else {
return nil
}
}
}
func createAddress() -> Address {
print("Function was called.")
let someAddress = Address()
someAddress.buildingNumber = "29"
someAddress.street = "Acacia Road"
return someAddress
}
3 通過可選鏈?zhǔn)秸{(diào)用訪問屬性
let andy = Person2()
andy.residence?.address = createAddress() // andy.residence結(jié)果為nil,可選鏈?zhǔn)秸{(diào)用失敗時(shí)饰序,等號右側(cè)的代碼不會被執(zhí)行泉褐。
4 通過可選鏈?zhǔn)秸{(diào)用調(diào)用方法
if let firstRoomName = andy.residence?[0].name {
print("The first room name is \(firstRoomName).")
} else {
print("Unable to retrieve the first room name.")
}
let andyHouse = Residence2()
andyHouse.rooms.append(Room(name: "Living Room"))
andyHouse.rooms.append(Room(name: "Kitchen"))
andy.residence = andyHouse
if let firstRoomName = andy.residence?[0].name {
print("The first room name is \(firstRoomName).")
} else {
print("Unable to retrieve the first room name.")
}
5 訪問可選類型的下標(biāo)
var testScores = ["Dave": [86, 82, 84], "Bev": [79, 94, 81]]
testScores["Dave"]?[0] = 91
testScores["Bev"]?[0] += 1
testScores["Brian"]?[0] = 72
6 連接多層可選鏈?zhǔn)秸{(diào)用
if let andyStreet = andy.residence?.address?.street {
print("John's street name is \(andyStreet).")
} else {
print("Unable to retrieve the address.")
}
let andyAddress = Address()
andyAddress.buildingName = "東方明珠"
andyAddress.street = "南京東路100號"
andy.residence?.address = andyAddress
if let andyStreet = andy.residence?.address?.street {
print("John's street name is \(andyStreet).")
} else {
print("Unable to retrieve the address.")
}
7 在方法的可選返回值上進(jìn)行可選鏈?zhǔn)秸{(diào)用
if let buildingIdentifier = andy.residence?.address?.buildingIdentifier() {
print("John's building identifier is \(buildingIdentifier).")
}
if let beginsWithThe =
andy.residence?.address?.buildingIdentifier()?.hasPrefix("東") {
if beginsWithThe {
print("John's building identifier begins with \"東\".")
} else {
print("John's building identifier does not begin with \"東\".")
}
}
playground文件在andyRon/LearnSwift