Swift Learning Notes

Optional Chaining

2021-08-18  本文已影响0人  宋奕Ekis

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!

上一篇 下一篇

猜你喜欢

热点阅读