Roggie
Roggie

Reputation: 1217

I get an error 'String' cannot match values in my Switch statement using swift, why?

I am getting the following error when trying to use a switch statement on currentState which of type MJMaterialSwitchState in the delegate method switchStateChanged below.

  Error: xpression pattern of type 'String' cannot match values of type 'MJMaterialSwitchState'

I am using a custom MJMaterialSwitch UI which works perfectly. It toggles between an on and off state

Function:

func switchStateChanged(_ switcher: MJMaterialSwitch, currentState: MJMaterialSwitchState) {

        tapticGenerator.notificationOccurred(.success)

            switch currentState{

                case "on":
                    discoverable = true

                case "off":
                    discoverable = false

                default:
                    break
            }

}

This is the MJMaterialSwitchState:

public enum MJMaterialSwitchState {
    case on, off
}

Upvotes: 0

Views: 197

Answers (1)

Rakesha Shastri
Rakesha Shastri

Reputation: 11242

You switch cases should be MJMaterialSwitchState instead of String values.

switch currentState {
    case .on:
        discoverable = true
    case .off:
        discoverable = false
}

Also, you do not need a default case if you cover all the cases in an enum.

Upvotes: 2

Related Questions