在对两个布尔值执行按位运算时出错。 错误:"二元运算符' | ='不能适用于两个Bool'操作数"
func checkAvailability(available:Bool) -> Bool{
var bChanged = false
bChanged |= available //"Binary operator '|=' cannot be applied to two 'Bool' operands"
return bChanged
}
请任何人帮我解决问题......
答案 0 :(得分:5)
您可以通过重载运算符来自己定义:
斯威夫特2:
func |= (inout left: Bool, right: Bool) {
left = left || right
}
斯威夫特3:
func |= (left: inout Bool, right: Bool) {
left = left || right
}
答案 1 :(得分:3)
这是Lew回答的简单扩展,包括其他两个“缺失”的运营商。
// 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
}