【发布时间】:2017-11-23 23:23:23
【问题描述】:
你经常有这样的 Int 枚举:
enum Difficulty: Int {
case Easy = 0
case Normal
case Hard
}
Difficulty 值有一定的含义,我们可能想为它们引入顺序。例如,我们需要比较的地方:
let isBonusAvailable = level.difficulty.rawVAlue <= Difficulty.Hard.rawValue
我想让这段代码更短一点:
let isBonusAvailable = level.difficulty <= .Hard
如果我将<= 直接添加到Difficulty,它可以轻松实现。但是我想总体上解决这个问题,所以我尝试了这个超级棘手的方法:
protocol RawRepresentableByInt {
var rawValue: Int { get }
}
extension RawRepresentableByInt {
static func <(lhs: RawRepresentableByInt, rhs: RawRepresentableByInt) -> Bool {
return lhs.rawValue < rhs.rawValue
}
static func >(lhs: RawRepresentableByInt, rhs: RawRepresentableByInt) -> Bool {
return lhs.rawValue > rhs.rawValue
}
static func <=(lhs: RawRepresentableByInt, rhs: RawRepresentableByInt) -> Bool {
return lhs.rawValue <= rhs.rawValue
}
static func >=(lhs: RawRepresentableByInt, rhs: RawRepresentableByInt) -> Bool {
return lhs.rawValue >= rhs.rawValue
}
}
// Error: Extension of protocol 'RawRepresentable' cannot have an inheritance clause
extension RawRepresentable: RawRepresentableByInt where RawValue == Int {
}
它会产生编译器错误:
错误:协议“RawRepresentable”的扩展不能有继承子句
我认为在逻辑上与Int enum 相比没有什么不可实现的。请帮我欺骗 Swift 编译器。任何也需要此类扩展的人都可以参与。
【问题讨论】:
标签: swift enums integer compare extension-methods