I tried a custom CharacterSet to check if the user input line contains any characters without a decimal digit. I use CharacterSet.decimalDigits and intersect it with user input. If this intersection is empty, this apparently means that the user has not entered valid input. However, the intersection is not empty.
let digits = CharacterSet.decimalDigits let letters = CharacterSet(charactersIn: "abcd") // never prints let intersection = digits.intersection(letters) for c in "abcd".characters { if intersection.contains(UnicodeScalar(String(c))!) { print("contains \(c)") // never prints } } for i in 0...9 { if intersection.contains(UnicodeScalar(String(i))!) { print("contains \(i)") } } print("intersection is empty: \(intersection.isEmpty)") // prints false
I even tried to loop on all unicode scalars to check my membership, and that doesn't print anything.
for i in 0x0000...0xFFFF { guard let c = UnicodeScalar(i) else { continue } if intersection.contains(c) { print("contains \(c)") } }
Why is set not empty?
Note Using let digits = CharacterSet(charactersIn: "1234567890") works as expected. I know that decimalDigits contains more than just 0-9, but the intersection should be empty.
source share