16:swift-可选链
正文
/* 可选链 1:可选链 1.1:可选链是一个调用和查询可选属性、方法和下标的过程,它可能为 nil。 1.2:如果可选项包含值,属性、方法或者下标的调用成功;如果可选项是 nil ,属性、方法或者下标的调用会返回 nil 。 1.3:多个查询可以链接在一起,如果链中任何一个节点是 nil ,那么整个链就会得体地失败。 */ import UIKit class OptionalChain16VC: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = .white self.title = "16:可选链" visitProperty() visitFunc() visitSubType() } // 1: 通过可选链访问属性 public func visitProperty() { 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.") } } // 2:通过可选链调用方法 public func visitFunc() { let john = Person() 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.") } } public func visitSubType() { let john = Person() if let firstRoomName = john.residence?[0].name { print("The first room name is \(firstRoomName).") } else { print("Unable to retrieve the first room name.") } // 赋值 residence属性 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.") } /* 3:**** 如果你尝试通过可选链取回一个 Int 值,就一定会返回 Int? ,不论通过了多少层的可选链; 类似地,如果你尝试通过可选链访问 Int? 值, Int? 一定就是返回的类型,无论通过了多少层的可选链。 */ if let johnsStreet = john.residence?.address?.street { print("John's street name is \(johnsStreet).") } else { print("Unable to retrieve the address.") } // 4: 链的多层连接 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.") } // 5:用可选返回值链接方法 // 如果你要进一步对方法的返回值进行可选链,在方法 buildingIdentifier() 的圆括号后面加上可选链问号 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\".") } } } } 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 buildingNumber != nil && street != nil { return "\(buildingNumber) \(street)" } else { return nil } } }
浙公网安备 33010602011771号