Yuan Fu
Yuan Fu

Reputation: 308

Objective-C to Swift Bitwise XOR equal Operator ^=

I have got this kind of code in Objective-C, and I want to convert to Swift

+ (BOOL)isLocationOutOfChina:(CLLocationCoordinate2D)location {
  BOOL oddFlag = NO;
  oddFlag ^= (a.x +
                  (c.y - a.y) /
                  (b.y - a.y) *
                  (b.x - a.x) <
                  point.x);
  return !oddFlag;
}

I have no idea of how to deal with Bitwise XOR equal Operator to swift ^=

I have got an error for this convert:

Binary operator ^= cannot be applied to two 'Bool' operands

Upvotes: 0

Views: 362

Answers (1)

rmaddy
rmaddy

Reputation: 318904

In Swift, the ^ operator (and hence, the ^= operator) only works with integer data types (such as Int). It doesn't work with Bool.

But note that your use of ^= in your Objective-C code is pointless. You always start with NO and then XOR that with either YES or NO. The result will be always be the value of the right-hand side. In other words NO ^ X will always be X. So simply do:

BOOL oddFlag = (a.x + ...);

in Objective-C. Then the Swift translation is simple.

Upvotes: 2

Related Questions