0

Having the following enumeration

enum ColliderType: UInt8 {
    case Hero = 0b1
    case GoblinOrBoss = 0b10
    case Projectile = 0b100
    case Wall = 0b1000
    case Cave = 0b10000
}

I'm trying to do something very simple:

    let combined = ColliderType.Hero.toRaw() | ColliderType.Wall.toRaw()

    // test 'combined' for the 'Wall' bitmask
    if (combined & ColliderType.Wall.toRaw()) { // Compliation error :[

    }

The error I'm getting is as follows

Type 'UInt8' does not conform to protocol 'BooleanType'

Shai
  • 25,159
  • 9
  • 44
  • 67
  • the bitwise operator does not return a _boolean_ value for you, but – in your case – an `UInt8` instead, so you may want to compare that number with someone (`>0`) to get a boolean value eventually for the `if` statement. – holex Oct 29 '14 at 11:43

2 Answers2

2
if (combined & ColliderType.Wall.toRaw()) != 0 { // No Compliation error :[

The expression in an if statement must be Boolean. UInt8 isn't a boolean. Pointers are not booleans. Optionals are not booleans. Comparisons give booleans.

This enforces good coding practices; you are forced to write the code that you should have written in C or C++ or Objective-C.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
2

It's because combined & ColliderType.Wall.toRaw() returns UInt8 while if is expecting BooleanType.

if (combined & ColliderType.Wall.toRaw()) != 0 {
}

should work.

rintaro
  • 51,423
  • 14
  • 131
  • 139