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

I want to avoid the double nested if.

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

but I get the unresolved identifier compiler error if I do that.

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?

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
A O
  • 5,516
  • 3
  • 33
  • 68

1 Answers1

5

You can do it this way:

if let popupButton = result?.control as? NSPopUpButton, popupButton.numberOfItems <= 1 {
    //blahblah
}
Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36
  • yup that did it! do you happen to know the term for the comma here? e.g, `&&` is a `conditional AND` – A O Jul 25 '17 at 20:51
  • I don't know if there is a term for it, but I just made a quick look on [Swift documentation on optional binding](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID333) and didn't find a term. – Mo Abdul-Hameed Jul 25 '17 at 20:55
  • 5
    the `,` is used to form a *multi-clause condition*. This used to be done with the `where` keyword; if you use `where` instead of the comma, the error message mentions using a `,` to form a multi-clause condition. So I guess that is Swift's term for it. – vacawama Jul 25 '17 at 20:55