Swift 1.2 (Xcode 6.3) removed xor '^' operator for Bool value?

北慕城南 提交于 2019-12-23 06:53:44

问题


This sample code on Xcode 6.3 ...

var str1 = ""
var str2 = ""
if str1.isEmpty ^ str2.isEmpty {
  // do something.
}

displays the following error.

'^' is unavailable: use the '!=' operator instead

I cannot find the spec in Apple documentation. Is this specification (and I'll have to lump it)?


回答1:


It's clearly intentional:

$ echo ':print_module Swift' | swift -deprecated-integrated-repl | fgrep "use the '!=' operator instead"

shows:

@availability(*, unavailable, message="use the '!=' operator instead") func ^=(inout lhs: Bool, rhs: Bool)
@availability(*, unavailable, message="use the '!=' operator instead") func ^(lhs: Bool, rhs: Bool) -> Bool



回答2:


Assuming you are trying to use a logical XOR, a != should serve your purpose. The ^ is a bitwise XOR. So makes sense that Apple removed it for bool values.



来源:https://stackoverflow.com/questions/29555569/swift-1-2-xcode-6-3-removed-xor-operator-for-bool-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!