Home > Software design >  Swift how to use inline if else conditional statements with or (||) operator
Swift how to use inline if else conditional statements with or (||) operator

Time:07-20

I have a simple block of code that I am trying to figure out how to add or operator in my if else statement

mode can be allEndPoints, none or direct. I am hoping for the following: if mode is equal to allEndPoints or none then [] else anything it should be equal to [selection].

What I have tried below however I get issue: Cannot convert value of type 'String' to expected argument type 'Bool'

let mode: String = "allEndPoints"
let selection:NSNumber = 501

try manager.add(
     toOut: mode == "allEndPoints" || "none" ? [] : [selection],
)

CodePudding user response:

After the || just add mode == "none"

mode == "allEndPoints" || mode == "none" ? [] : [selection]

CodePudding user response:

What you think the || operator might do is not a thing it will do, and that's not particular to Swift.

endpoint is a single word.

enum Mode {
  case allEndpoints
  case direct
  case none
}

let mode = Mode.allEndpoints

[.allEndpoints, .none].contains(mode) ? [] : [selection]
  • Related