ios Swift if 语句 - 多个条件以逗号分隔?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35434939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift if statement - multiple conditions separated by commas?
提问by Marcus Leon
Looking at a Swift example:
看一个 Swift示例:
if let sourceViewController = sender.sourceViewController as? MealViewController, meal = sourceViewController.meal {
...
}
The doc states:
该文档指出:
... the code assigns that view controller to the local constant sourceViewController, and checks to see if the meal property on sourceViewController is nil.
...代码将该视图控制器分配给本地常量 sourceViewController,并检查 sourceViewController 上的 meal 属性是否为零。
Question: Does Swift let you have multiple conditions in your if statement when separated by commas (as in this example with the comma after MealViewController
)?
问题:当用逗号分隔时,Swift 是否允许您在 if 语句中有多个条件(如本示例中的 后有逗号MealViewController
)?
Haven't seen this in the docs.
在文档中没有看到这个。
回答by Luca Angeletti
Yes when you write
是的,当你写
if let a = optA, let b = optB, let c = optC {
}
Swift does execute the body of the IF
only if all the assignments are properly completed.
IF
只有当所有的分配都正确完成时,Swift 才会执行 的主体。
More
更多的
Another feature of this technique: the assignments are done in order.
这种技术的另一个特点是:分配是按顺序完成的。
So only if a value is properly assigned to a
, Swift tries to assign a value to b
. And so on.
因此,只有当一个值被正确分配给 时a
,Swift 才会尝试为 分配一个值b
。等等。
This allows you to use the previous defined variable/constant like this
这允许您像这样使用先前定义的变量/常量
if let a = optA, let b = a.optB {
}
In this case (in second assignment) we are safely using a
because we knowthat if that code is executed, then a
has been populated with a valid value.
在这种情况下(在第二次赋值中),我们可以安全地使用,a
因为我们知道如果执行该代码,a
则已填充了有效值。
回答by Joachim Isaksson
Yes. Swift: Documentation: Language Guide: The Basics: Optional Bindingsays:
You can include as many optional bindings and Boolean conditions in a single
if
statement as you need to, separated by commas. If any of the values in the optional bindings arenil
or any Boolean condition evaluates tofalse
, the wholeif
statement's condition is considered to befalse
. The followingif
statements are equivalent:if let firstNumber = Int("4"), let secondNumber = Int("42"), firstNumber < secondNumber && secondNumber < 100 { print("\(firstNumber) < \(secondNumber) < 100") } // Prints "4 < 42 < 100" if let firstNumber = Int("4") { if let secondNumber = Int("42") { if firstNumber < secondNumber && secondNumber < 100 { print("\(firstNumber) < \(secondNumber) < 100") } } } // Prints "4 < 42 < 100"
您可以根据需要在单个
if
语句中包含任意数量的可选绑定和布尔条件,并用逗号分隔。如果可选绑定中的nil
任何值是或任何布尔条件的计算结果为false
,则整个if
语句的条件被视为false
。以下if
语句是等效的:if let firstNumber = Int("4"), let secondNumber = Int("42"), firstNumber < secondNumber && secondNumber < 100 { print("\(firstNumber) < \(secondNumber) < 100") } // Prints "4 < 42 < 100" if let firstNumber = Int("4") { if let secondNumber = Int("42") { if firstNumber < secondNumber && secondNumber < 100 { print("\(firstNumber) < \(secondNumber) < 100") } } } // Prints "4 < 42 < 100"