Raw value for enum case must be a literal

后端 未结 4 1136
花落未央
花落未央 2021-02-06 22:38

I have this enum:

enum GestureDirection:UInt {
    case Up =       1 << 0
    case Down =     1 << 1
    case Left =     1 << 2
    case Right          


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-06 23:04

    That's because 1 << 0 isn't a literal. You can use a binary literal which is a literal and is allowed there:

    enum GestureDirection:UInt {
        case Up =       0b000
        case Down =     0b001
        case Left =     0b010
        case Right =    0b100
    }
    

    Enums only support raw-value-literals which are either numeric-literal (numbers) string-literal­ (strings) or boolean-literal­ (bool) per the language grammar.

    Instead as a workaround and still give a good indication of what you're doing.

提交回复
热议问题