[英]Is it possible to use the variable from an optional binding within the same conditional statement?
if let popupButton = result?.control as? NSPopUpButto {
if popupButton.numberOfItems <= 1 {
// blahblah
}
}
I want to avoid the double nested if. 我想避免双嵌套if。
if let popupButton = result?.control as? NSPopUpButton && popupButton.numberOfItems <= 1 {}
but I get the unresolved identifier
compiler error if I do that. 但是如果这样做,我会得到unresolved identifier
编译器错误。
Is there any way to make this condition on one line? 有什么办法可以使这一条件成为一行? Or because I'm using an optional binding, am I forced to make a nested if
here? 还是因为我使用的是可选绑定, if
在这里,我是否被迫嵌套?
You can do it this way: 您可以这样操作:
if let popupButton = result?.control as? NSPopUpButton, popupButton.numberOfItems <= 1 {
//blahblah
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.