是否可以在同一条件语句中使用来自可选绑定的变量?

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
    }
}

我想避免双重嵌套 if。

if let popupButton = result?.control as? NSPopUpButton && popupButton.numberOfItems <= 1 {}

但如果我这样做,我会得到 unresolved identifier 编译器错误。

有什么办法可以把这个条件写在一条线上吗?或者因为我使用的是可选绑定,我是否被迫在此处嵌套 if

你可以这样做:

if let popupButton = result?.control as? NSPopUpButton, popupButton.numberOfItems <= 1 {
    //blahblah
}