4

I am developing iOS app in Swift 3 and I am face with a problem. I have an enum:

enum MapMode: Equatable {
    case scenes
    case temperature
    case editingScene
    case editingWaterfall(waterfall: Waterfall, mode: PumpEditorMode, waterfallScene: WaterfallScene?)
    case editingWaterfallScenes(waterfall: Waterfall)
    case editingTemperature(thermostate: Thermostat)
    case editingShutter(shutter: Device, doorDirection: DoorDirection)
    case editingDoor
    case comfortScenesList

    public static func ==(lhs: MapMode, rhs: MapMode) -> Bool {
        switch (lhs, rhs) {
        case let (.editingTemperature(ther01), .editingTemperature(ther02)):
            return ther01.deviceId ?? 0 == ther02.deviceId ?? 0
        case let (a, b) where a.rawValue == b.rawValue:
            return true
        default:
            return false
        }
    }
}

and I get an error Value of type 'MapMode' has no member 'rawValue' so I realize I need add raw values for enums so I edit a code to this:

enum MapMode: Int, Equatable {
    case scenes = 0
    case temperature = 1
    case editingScene = 2
    case editingWaterfall(waterfall: Waterfall, mode: PumpEditorMode, waterfallScene: WaterfallScene?) = 3
    case editingWaterfallScenes(waterfall: Waterfall) = 4
    case editingTemperature(thermostate: Thermostat) = 5
    case editingShutter(shutter: Device, doorDirection: DoorDirection) = 6
    case editingDoor = 7
    case comfortScenesList = 8

    public static func ==(lhs: MapMode, rhs: MapMode) -> Bool {
        switch (lhs, rhs) {
        case let (.editingTemperature(ther01), .editingTemperature(ther02)):
            return ther01.deviceId ?? 0 == ther02.deviceId ?? 0
        case let (a, b) where a.rawValue == b.rawValue:
            return true
        default:
            return false
        }
    }
}

And it is not compile too. Enum with raw type cannot have cases with arguments My question is: how to make enum with parameters to be an Equatable?

EDIT 1: Temporary(working) solution

enum MapMode: Equatable {
    case scenes
    case temperature
    case editingScene
    case editingWaterfall(waterfall: Waterfall, mode: PumpEditorMode, waterfallScene: WaterfallScene?)
    case editingWaterfallScenes(waterfall: Waterfall)
    case editingTemperature(thermostate: Thermostat)
    case editingShutter(shutter: Device, doorDirection: DoorDirection)
    case editingDoor
    case comfortScenesList

    public static func ==(lhs: MapMode, rhs: MapMode) -> Bool {
        switch (lhs, rhs) {
        case (.scenes, .scenes), (.temperature, .temperature), (.editingScene, .editingScene), (.editingDoor, .editingDoor), (.comfortScenesList, .comfortScenesList):
            return true
        case let (.editingTemperature(ther01), .editingTemperature(ther02)):
            return ther01.deviceId ?? 0 == ther02.deviceId ?? 0
        case let (.editingWaterfall(waterfall01, _, _), .editingWaterfall(waterfall02, _, _)):
            return waterfall01.deviceId == waterfall02.deviceId
        case let (.editingWaterfallScenes(waterfall01), .editingWaterfallScenes(waterfall02)):
            return waterfall01.deviceId == waterfall02.deviceId
        case let (.editingShutter(shutter01, _), .editingShutter(shutter02, _)):
            return shutter01.deviceId == shutter02.deviceId
        default:
            return false
        }
    }
}

I decided to abort rawValues and I expanded == function. This solution has a deffect. Ever time I add new case I need to remember to expand == function.

I am looking for more generic solution.

2
  • You can just specify the first value (= 0) and each successive value will increment Commented Feb 6, 2017 at 12:14
  • @Ashley Mills don't work it generates error message Enum with raw type cannot have cases with arguments Commented Feb 6, 2017 at 12:25

1 Answer 1

3

You might want to separate them into two enums:

enum MapMode: Equatable {
    case mapMode(mode:ModeType)
    case editingWaterfall(waterfall: Waterfall, mode: PumpEditorMode, waterfallScene: WaterfallScene?)
    case editingWaterfallScenes(waterfall: Waterfall)
    case editingTemperature(thermostate: Thermostat)
    case editingShutter(shutter: Device, doorDirection: DoorDirection)

    public static func ==(lhs: MapMode, rhs: MapMode) -> Bool {
        switch (lhs, rhs) {
        case let (.editingTemperature(ther01), .editingTemperature(ther02)):
            return ther01.deviceId ?? 0 == ther02.deviceId ?? 0
        case let (mapMode(a), mapMode(b)) where a.rawValue == b.rawValue:
            return true
        default:
            return false
        }
    } 
}

enum ModeType: Int, Equatable {
    case scenes = 0
    case temperature
    case editingScene
    case editingWaterfall
    case editingWaterfallScenes
    case editingTemperature
    case editingShutter
    case editingDoor
    case comfortScenesList

    public static func ==(lhs: ModeType, rhs: ModeType) -> Bool {
        switch (lhs, rhs) {
        case let (a, b) where a.rawValue == b.rawValue:
            return true
        default:
            return false
        }
    } 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.