Question about Chapter 9 challenge

I was wondering if someone could explain this line from Chapter 9 (Page 240) a little bit further.

let labelNode = contact.bodyA.categoryBitMask ==
PhysicsCategory.Label ? contact.bodyA.node :
contact.bodyB.node

I was able to complete the challenge first without the guidelines as suggested. I decided to take it one step further, and try it again with the suggested directions. I was able to implement it and complete the challenge, but was wondering if someone could offer a bit more detail about this.

Thanks.

@rudenik - That’s a ternary operator.

let a = 1 == 2 ? 3 : 4

That is equivalent to:

let a: Int
if 1 == 2 {
  a = 3
} else {
  a = 4
}

So your code is the equivalent of:

let labelNode: SKNode
if contact.bodyA.categoryBitMask == PhysicsCategory.Label {
  labelNode = contact.bodyA.node
} else {
  labelNode = contact.bodyB.node
}
1 Like

Awesome explanation that you very much.