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!

相关文章

  • Swift - 可选型操作

    Optional Chaining 与 Nil Coalesce 操作 optional chaining 对象?...

  • ES新特性

    optional chaining 本质:语法糖 年份:2020 optional chaining 可选链。主要...

  • 11-可选链

    可选链(Optional Chaining)

  • Swift中的Optional

    1、隐式解包Optional2、Optional Chaining3、多重Optional4、Optional M...

  • Optional Chaining

    Calling Methods Through Optional Chaining In the example ...

  • Optional Chaining

    Optional Chaining自判断链,可以用来判断调用的目标属性、方法、附属脚本等是否为空。不为空为成功,为...

  • Optional Chaining

    使用 Optional Chaining 可以让我们摆脱很多不必要的判断和取值,但是在使用的时候需要小心陷阱。 因...

  • swift入门16 可选择链

    Optional chaining is a process for querying and calling p...

  • es2020可选链语法与合并空语法babel

    npm install @babel/plugin-proposal-optional-chaining ...

  • Swift - Optional Chaining

    Swift 的可选绑定类似于 OC 中的消息为 nil,但是运用于所有类型,能够被检测成功或失败 Query & ...

网友评论

    本文标题:Optional Chaining

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