Optional Chaining

作者: 宋奕Ekis | 来源:发表于2021-08-18 09:44 被阅读0次

    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!

    相关文章

      网友评论

        本文标题:Optional Chaining

        本文链接:https://www.haomeiwen.com/subject/nhxgbltx.html