So, in iOS Swift, we can make an optional chain to simplify the nil check, as in the official documentation.
let johnsAddress = Address() johnsAddress.buildingName = "The Larches" johnsAddress.street = "Laurel Street" john.residence!.address = johnsAddress if let johnsStreet = john.residence?.address?.street { println("John street name is \(johnsStreet).") } else { println("Unable to retrieve the address.") } // prints "John street name is Laurel Street."
I understand the use of an optional chain in john.residence?.address?.street , but how do I know where the chain actually breaks (if either residence or address is nil ). Can we determine if there is residence or address nil , or should we check it again with if-else ?
swift optional
Niko Adrianus Yuwono
source share