1

I have a variable of type UIRectEdge that I initiated like that:

var edges: UIRectEdge = .Right | .Left

Later on, I need to know what does this variable contain. How can I run through it?

Nico
  • 6,269
  • 9
  • 45
  • 85

1 Answers1

2

If you want to just find out if the variable contains a certain component you can go like so:

    var edges: UIRectEdge = .Right | .Left | .Top

    if(edges & UIRectEdge.Right == UIRectEdge.Right){
        println("right")
    }

    if(edges & UIRectEdge.Left == UIRectEdge.Left){
        println("left")
    }

    if(edges & (UIRectEdge.Left | UIRectEdge.Top) == (UIRectEdge.Left | UIRectEdge.Top)){
        println("left and top")
    }

Here's why this works:

Say for instance left is 00100 (in binary), and right is 01000. Combining them with the OR operator (the pipe) makes 01100.

Then, using the AND operator 01100(combined) & 01000(right) will find where both those binary numbers are both 1, and make that bit a 1, which would be the same as the original number (right):

01100 (.Right | .Left)   & 
01000   (.Right) =
01000   (.Right)

Note these are the binary operators & and |; not the logical && and ||.

Fonix
  • 11,447
  • 3
  • 45
  • 74
  • ok I didn't realised it worked this way, then it makes sense that I cannot loop through it. Thanks. – Nico Jul 15 '15 at 05:01