gpt4 book ai didi

ios - Swift if 语句 - 多个条件用逗号分隔?

转载 作者:IT王子 更新时间:2023-10-29 05:06:30 27 4
gpt4 key购买 nike

看 Swift example :

if let sourceViewController = sender.sourceViewController as? MealViewController, meal = sourceViewController.meal {
...
}

文档指出:

... the code assigns that view controller to the local constant sourceViewController, and checks to see if the meal property on sourceViewController is nil.

问题:Swift 是否允许您在 if 语句中使用逗号分隔的多个条件(如本例中 MealViewController 后的逗号)?

没有在文档中看到这个。

最佳答案

写的时候是的

if let a = optA, let b = optB, let c = optC {

}

Swift 确实执行了 IF 的主体仅当正确完成所有可选绑定(bind)

更多

此技术的另一个特点:分配是按顺序完成的。

所以只有当一个值被正确分配给a , Swift 尝试给 b 赋值.等等。

这允许你像这样使用之前定义的变量/常量

if let a = optA, let b = a.optB {

}

在这种情况下(在第二个作业中)我们安全地使用 a因为我们知道如果该代码被执行,那么a已填充有效值。

关于ios - Swift if 语句 - 多个条件用逗号分隔?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35434939/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com