I recently spent a couple of hours trying to figure out why code like this never did what I wanted it to do:
var opts: NSString.CompareOptions = [.caseInsensitive, .diacriticInsensitive]
....
if opts ~= .diacriticInsensitive {
print("Using diacritics") <<<<< Never called
}
It should have been:
if opts.contains(.diacriticInsensitive) {
print("Using diacritics")
}
Having been doing a bunch of this kind of thing recently:
if 200...299 ~= statusCode {...}
It seemed only natural to use the ~= operator instead of using contains on the OptionSet.
Normal Set is prevented from compiling such a thing:
var opts = Set([1, 2])
if opts ~= 2 { // Fails to compile - Cannot convert value of type 'Int' to expected argument type 'Set<Int>'
}
Can anyone suggest a way to prevent this mistake again as its a bit of a gotcha?
It's calling the func ~= <T>(a: T, b: T) -> Bool where T : Equatable implementation that ships with the Standard Library, which is why it doesn't work. Perhaps what you can do is provide a custom implementation but with an @available annotation to discourage its use and promote the usage of .contains. For example:
~= is also used during pattern matching of values (e.g. in a switch/case). I think it could make sense to define this for option set, I'd recommend bringing it up on the evolution forum
I delete DerivedData and restart Xcode and now it compile. But it doesn't print, so what's is it suppose to do?
let statusCode = 150
if 200...299 ~= statusCode {
print("statusCode is in range 111") // do not print
}
if case 200...299 = statusCode {
print("statusCode is in range 222") // do not print
}