I was trying to user CharacterSet to check if a user input string contains any non decimal digit characters. I use CharacterSet.decimalDigits
and take the intersection of that with the user input. If this intersection is empty, it presumably means the user hasn't entered valid input. Yet the intersection is not empty.
我试图使用CharacterSet来检查用户输入字符串是否包含任何非十进制数字字符。我使用CharacterSet.decimalDigits并将其与用户输入的交集。如果此交集为空,则可能意味着用户未输入有效输入。但十字路口并非空洞。
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 looping over all unicode scalars to test for membership, and that doesn't print anything.
我甚至尝试循环遍历所有unicode标量来测试成员资格,并且不会打印任何内容。
for i in 0x0000...0xFFFF {
guard let c = UnicodeScalar(i) else {
continue
}
if intersection.contains(c) {
print("contains \(c)")
}
}
Why is the set non empty?
为什么集合非空?
Note Using let digits = CharacterSet(charactersIn: "1234567890")
works as expected. I know that the decimalDigits
contains more than just 0-9, but the intersection should still be empty.
注意使用let digits = CharacterSet(charactersIn:“1234567890”)按预期工作。我知道decimalDigits包含的不仅仅是0-9,但是交集应该仍然是空的。
1 个解决方案
#1
0
I browsed the CharacterSet
bugs and didn't see one around intersections incorrectly reporting isEmpty
so if you have the time you should file a bug since this is a nice reproducible example.
我浏览了CharacterSet错误并且没有看到交叉点周围的错误报告isEmpty,所以如果你有时间你应该提交bug,因为这是一个很好的可重现的例子。
In the mean time, you could try this to check to see if the input contains any characters from .decimalDigits
characterSet:
同时,你可以尝试这个来检查输入是否包含.decimalDigits characterSet中的任何字符:
let letterInput = CharacterSet(charactersIn: "abcd")
digits.isSubset(of: letterInput.inverted)
// -> true
let letterAndDigitInput = CharacterSet(charactersIn: "abcd 1234")
digits.isSubset(of: letterAndDigitInput.inverted)
// -> false
let digitInput = CharacterSet(charactersIn: "1234")
digits.isSubset(of: digitInput.inverted)
// -> false
#1
0
I browsed the CharacterSet
bugs and didn't see one around intersections incorrectly reporting isEmpty
so if you have the time you should file a bug since this is a nice reproducible example.
我浏览了CharacterSet错误并且没有看到交叉点周围的错误报告isEmpty,所以如果你有时间你应该提交bug,因为这是一个很好的可重现的例子。
In the mean time, you could try this to check to see if the input contains any characters from .decimalDigits
characterSet:
同时,你可以尝试这个来检查输入是否包含.decimalDigits characterSet中的任何字符:
let letterInput = CharacterSet(charactersIn: "abcd")
digits.isSubset(of: letterInput.inverted)
// -> true
let letterAndDigitInput = CharacterSet(charactersIn: "abcd 1234")
digits.isSubset(of: letterAndDigitInput.inverted)
// -> false
let digitInput = CharacterSet(charactersIn: "1234")
digits.isSubset(of: digitInput.inverted)
// -> false