Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS:Binary operator '|=' cannot be applied to two 'Bool' operands

Tags:

ios

xcode7

swift2

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

like image 949
Kiran P Nair Avatar asked Jun 01 '26 17:06

Kiran P Nair


2 Answers

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
}
like image 164
Lew Avatar answered Jun 03 '26 11:06

Lew


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
}
like image 21
RenniePet Avatar answered Jun 03 '26 13:06

RenniePet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!