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?
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 ||
.