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

2

19

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)?

Madi answered 10/4, 2015 at 7:20 Comment(4)
I cannot find it in swiftdoc.org neither in the header files, so probably yes.Grandfather
The error message told you how to fix the problem.Hubble
Yes. I know it. Currently the link of release notes is dead link, so I wanted to know if it is a specification's change.Madi
IMO Swift should have a "^^" operator because "!=" is not nearly as intuitive. Read: "one but not the other" vs. "is not equal to". "!=" works, but it's more obscure.Seizure
G
14

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
Gorden answered 10/4, 2015 at 14:2 Comment(0)
P
30

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.

Pilgrim answered 10/4, 2015 at 7:39 Comment(4)
So how do you apply XOR to three booleans simultaneously?Hance
@Hance xor has no definition for more than two elements at the same time, how would you expect it to work?Terpsichore
@Hance (a != b) != c should do it.Selfdeception
@Hance XOR is associative. (a ⊕ b) ⊕ c = a ⊕ (b ⊕ c), so you can pick either oneElectrophone
G
14

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
Gorden answered 10/4, 2015 at 14:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.