我在Swift 3中开发iOS应用程序,我遇到了问题。我有一个枚举:
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
}
}
}
我收到错误Value of type 'MapMode' has no member 'rawValue'
所以我意识到我需要为枚举添加原始值,所以我编辑了一个代码:
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
}
}
}
它也不是编译。 Enum with raw type cannot have cases with arguments
我的问题是:如何使参数枚举成为Equatable?
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
}
}
}
我决定中止rawValues
并扩展==
功能。这个解决方案有效。我有时候添加新case
我需要记住扩展==
功能。
我正在寻找更通用的解决方案。
答案 0 :(得分:3)
您可能希望将它们分成两个枚举:
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
}
}
}