Optional Chaining

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!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容