Declaring and using a bit field enum in Swift

前端 未结 14 1990
余生分开走
余生分开走 2020-12-13 09:24

How should bit fields be declared and used in Swift?

Declaring an enum like this does work, but trying to OR 2 values together fails to compile:

enum         


        
14条回答
  •  离开以前
    2020-12-13 09:40

    This worked for me.

    • 1 << 0 //0000
    • 1 << 1 //0010
    • 1 << 2 //0100
    • 1 << 3 //1000

        enum Collision: Int {
          case Enemy, Projectile, Debris, Ground
          func bitmask() -> UInt32 {
              return 1 << self.rawValue
            }
        }
      

提交回复
热议问题