-1

I have enum named PinpadModelCallback, declared like this:

enum PinpadModelCallback {
    case Connecting
    case SelectDevice
    case DevicesUpdated
    case Configuring
    case Stopping
    case Stopped
    case CardWaiting
    case TerminalProcessing
    case TransactionStarted
    case SignatureRequest
    case ApplicationSelection
    case TransactionSuccess(result: LTransactionResult)
    case TransactionError(msg: String)
    case AuthError(msg: String)
}

I have function in which i pass enum type as an argument and try to check it:

private func setInstuction(_ msg: String, callback: PinpadModel.PinpadModelCallback? = nil) {
        guard let callback = callback else { return }
        if callback == .Connecting {

        }

The following code produce an error:

Binary operator '==' cannot be applied to operands of type 'PinpadModel.PinpadModelCallback' and '_'

It work with switch case:

  switch callback {
    case .Connecting:
        break
    default:
        break
    }

But i want to use if else. Why it's produce an error?

DDelforge
  • 558
  • 7
  • 18
Evgeniy Kleban
  • 6,794
  • 13
  • 54
  • 107
  • 1
    Not an answer, but if you are coding in swift 3.0 or later, enums should have lowercase capital. – Vollan Mar 20 '18 at 14:06

2 Answers2

2

As the error message tells you, the problem is the ==. You cannot use equals comparison on an enumeration that has associated values for any of its cases. Such an enum is not Equatable. Your enum has associated values for the last three of its cases.

Remove the associated values, or adopt Equatable and define == yourself, or use another mode of expression.

This will change in Swift 4.1, where you can declare your enum Equatable and get an autogenerated definition of ==. You can try it out by downloading the Xcode 9.3 beta.

matt
  • 515,959
  • 87
  • 875
  • 1,141
1

You can use if case for this

like

if case .Connecting = callback { 
    // True
} else {
   // false
}

You can also get var of enum

if case .TransactionError(let msg) = callback {
    print(msg)
} else {
   // No error
 }

Hope it is helpful

Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98