A. Dunlop - 9 months ago 34

Swift Question

i have replaced my code with x, y, and z.

At the moment my code runs like this but i have a lot of lines to check x. is there a way of instead of using this

`if x != y && x != z`

using something like this instead

`if x != y,z`

sorry everyone I should have explained it better. Here is a piece of the updated code using contains which was very helpful thank you!

`if contactCreation.checkAllowed(newContact.contactDetails){`

if{contactCreation.blockedCreation.contains(newContact.contactDetails){

watchList.addAttempt(newContact.contactDetails)

}

}else{

newAccount.showReasons()

}

I guess my question was more is there a way to check if x is y and if x is z but not if x is y + z. basically I wanted to call

`if x != y && x != z && x != d && != e`

without having to get x every-time in one if statement.

so something like this

`if x != y && != z && != d && != e`

thanks for all the help!

Answer Source

A slightly shorter version is to add an overload to the pattern matching operator (`~=`

):

```
func ~=<T: Equatable>(pattern: [T], value: T) -> Bool {
return pattern.contains(value)
}
if !([y,z] ~= x) {
}
```

If that expression looks a little unwieldy and you write it a lot, you can define your own operator:

```
infix operator !!= : ComparisonPrecedence
func !!=<T: Equatable>(lhs: T, rhs: [T]) -> Bool {
return !rhs.contains(lhs)
}
if x !!= [y,z] {
}
```