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.
Enum with raw type cannot have cases with arguments