枚举 – Swift如何指定类型约束为枚举?

我想指定类型约束,类型应该是一个原始值枚举:
enum SomeEnum: Int {
  case One,Two,Three
}

class SomeProtocol<E: enum<Int>> { // <- won't compile
    func doSomething(e: E) {
        compute(e.toRaw())
    }
}

我怎么能在斯威夫特呢? (我用F#语法为例)

enum SomeEnum: Int {
    case One,Three
}

class SomeClass<E: RawRepresentable where E.RawValue == Int>{
    func doSomething(e: E) {
        print(e.rawValue)
    }
}

class SomeEnumClass : SomeClass<SomeEnum> {

}

或直接

class SomeOtherClass{
    func doSomething<E: RawRepresentable where E.RawValue == Int>(e: E) {
        print(e.rawValue)
    }
}

swift3的更新:

enum SomeEnum: Int {
    case One,Three
}

class SomeClass<E: RawRepresentable> where E.RawValue == Int {
    func doSomething(e: E) {
        print(e.rawValue)
    }
}

class SomeEnumClass : SomeClass<SomeEnum> {

}

RESP。

class SomeOtherClass{
    func doSomething<E: RawRepresentable>(e: E) where E.RawValue == Int {
        print(e.rawValue)
    }
}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...