2
votes

I got an error while performing a bitwise operation on two boolean values. Error :"Binary operator '|=' cannot be applied to two 'Bool' operands"

func checkAvailability(available:Bool) -> Bool{
    var bChanged = false
    bChanged |= available //"Binary operator '|=' cannot be applied to two 'Bool' operands"
    return bChanged  
}

Please any one help me to solve the problem...

2
bChanged = bChanged || available - OOPer
@Droppy, just try. - OOPer
Well given that bChanged = false then it's the same as just returning available. - Droppy
@Droppy, I see. You take that part is sort of actual. But I take that part as just a simplified code which is describing bChanged is a Bool variable initialized with a certain value. Maybe we should have clarified such points. - OOPer
@BraneDullet when you add code, edit the question to add it, instead of mentioning it in the comments - fishinear

2 Answers

5
votes

You could define it yourself by overloading the operator:

Swift 2:

func |= (inout left: Bool, right: Bool) {
   left = left || right
}

Swift 3:

func |= (left: inout  Bool, right: Bool) {
   left = left || right
}
3
votes

This is a simple expansion of Lew's answer to include the other two "missing" operators.

// A couple of operators that exist in C# and Java but are missing from Swift.

public func |= (leftSide : inout Bool, rightSide : Bool) {
   leftSide = leftSide || rightSide
}

public func &= (leftSide : inout Bool, rightSide : Bool) {
   leftSide = leftSide && rightSide
}

public func ^= (leftSide : inout Bool, rightSide : Bool) {
   leftSide = leftSide != rightSide
}