2
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这里嵌套?

4

1 回答 1

5

你可以这样做:

if let popupButton = result?.control as? NSPopUpButton, popupButton.numberOfItems <= 1 {
    //blahblah
}
于 2017-07-25T20:49:46.177 回答