Swift 1.2 (Xcode 6.3) 删除了布尔值的 xor '^' 运算符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29555569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift 1.2 (Xcode 6.3) removed xor '^' operator for Bool value?
提问by Mitsuaki Ishimoto
This sample code on Xcode 6.3 ...
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)?
我在 Apple 文档中找不到规范。这是规范吗(我必须把它混为一谈)?
采纳答案by rintaro
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
回答by lostInTransit
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.
假设您正在尝试使用逻辑 XOR, a!=
应该可以满足您的目的。该^
是按位异或。因此,Apple 将其删除为 bool 值是有道理的。