I have the below code and it works fine, but I was expecting the line print(john.residence!.numberOfRooms)
to crash, as my numberOfRooms
variable has nil
value and I am not using forced wrapping when passing its value as argument to print()
. Can anyone please explain why is this not triggering a runtime error and printing nil
safely instead?
class Person {
var residence: Residence?
}
class Residence {
var numberOfRooms: Int?
}
let john = Person()
john.residence = Residence()
print(john.residence!.numberOfRooms)