How in Swift specify type constraint to be enum?

前端 未结 3 1796
情话喂你
情话喂你 2020-12-14 15:18

I want to specify a type constraint that the type should be a raw value enum:

enum SomeEnum: Int {
  case One, Two, Three
}

class SomeProtocol

        
3条回答
  •  無奈伤痛
    2020-12-14 15:31

    enum SomeEnum: Int {
        case One, Two, Three
    }
    
    class SomeClass{
        func doSomething(e: E) {
            print(e.rawValue)
        }
    }
    
    class SomeEnumClass : SomeClass {
    
    }
    

    or directly

    class SomeOtherClass{
        func doSomething(e: E) {
            print(e.rawValue)
        }
    }
    

    UPDATE for swift3:

    enum SomeEnum: Int {
        case One, Two, Three
    }
    
    class SomeClass where E.RawValue == Int {
        func doSomething(e: E) {
            print(e.rawValue)
        }
    }
    
    class SomeEnumClass : SomeClass {
    
    }
    

    resp.

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

提交回复
热议问题