Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 716 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
XOR in Swift 5?

#1
I'm trying to do an XOR operation in Swift 5. The documentation does not seem to mention explicitly doing it with two boolean values here:

[To see links please register here]


Is this possible? It says to use the `^` operation but I get the error when trying:

card != nil ^ appointment.instructor == nil

>ERROR Adjacent operators are in non-associative precedence group 'ComparisonPrecedence'
Reply

#2
You need to define `^` for `Bool` since it only exists for Ints. See the apple documentation [here][1].

Example:

```
import UIKit
import PlaygroundSupport

extension Bool {
static func ^ (left: Bool, right: Bool) -> Bool {
return left != right
}
}

let a = true
let b = false
print (a^b)
```


[1]:

[To see links please register here]

Reply

#3
The `^` operator is defined for integer types but not for `Bool`. You could add your own definition, but it's not strictly needed. The XOR operation on `Bool` is the same as the `!=` operation. Here's the truth tables for `A XOR B` and `A != B`:

A B A^B A!=B
F F F F
F T T T
T F T T
T T F F

So we could write your expression like this:

(card != nil) != (appointment.instructor == nil)

That is kind of hard to understand though. If the goal is to ensure that exactly one of the cases is true, I might write it like this for clarity:

[(card != nil), (appointment.instructor == nil)].filter({ $0 == true }).count == 1


Reply

#4
The documentation clearly states that `^` is the _bitwise XOR_ operator and since a `Bool` is only a single bit, bitwise XOR is not defined on it. If you put correct parentheses on your expression, you get the correct error message:

(card != nil) ^ (appointment.instructor == nil)

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

There's no XOR operator in Swift, so to do a XOR on two `Bool`s, you need to define your own XOR function or operator.

infix operator ^^
extension Bool {
static func ^^(lhs:Bool, rhs:Bool) -> Bool {
if (lhs && !rhs) || (!lhs && rhs) {
return true
}
return false
}
}

Tests:

let trueValue:Bool? = true
let falseValue:Bool? = false
let nilValue:Bool? = nil

(trueValue != nil) ^^ (nilValue != nil) // true
(trueValue != nil) ^^ (falseValue != nil) // false
(nilValue != nil) ^^ (nilValue != nil) // false
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through