Calling Methods Through Optional Chaining
func printNumberOfRooms() {
print("The number of rooms is \(numberOfRooms)")
}
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.")
}
// Prints "It was not possible to print the number of rooms."
In the example above, printNumberOfRooms()
is a method which doesn’t return any value, but in swift, even if there is no any return, the function do return a void
value, and in optional chaining, it will be void?
, and with caution, void
and void?
is not nii
, so we can use optional chaining to verify if the method call was successful.
This episode is familiar enough for me, and it is easy to understand even if this episode is so long.So no more need to be noted.
Let’s think!
网友评论