Home > Software design >  Is there shorthand conditional syntax in swift?
Is there shorthand conditional syntax in swift?

Time:05-11

Is this the shortest way to write this in swift?

if currentStep == .confirmSignInAttempt1 || currentStep == .confirmSignInAttempt2 || currentStep == .confirmSignInAttempt3

CodePudding user response:

You can use

if [.confirmSignInAttempt1,.confirmSignInAttempt2,.confirmSignInAttempt3].contains(currentStep)

CodePudding user response:

You can use a custom operator like:

if currentStep =|| [.confirmSignInAttempt1, .confirmSignInAttempt2, .confirmSignInAttempt3] { ... }

using a simple infix operator implementation

infix operator =||
func =||<T: Comparable>(lhs: T, rhs: [T]) -> Bool { rhs.contains(lhs) }
  • Related