Fondation
具有CharacterSet
结构(桥接到NSCharacterSet(来管理字符集,例如在使用Formatter
实例时。令人惊讶的是,CharacterSet 不是一个集合,尽管功能和目的完全相同。不幸的是,CharacterSet 不是集合以太,所以现在我不知道如何检索它的元素。
// We can initialize with String
let wrongCharacterSet = CharacterSet(charactersIn: "0123456789").inverted
// but how can we get the characters back ?
var chSet = CharacterSet.decimalDigits
let chString = String(chSet) // doesn't work
let chS = Set(chSet) // doesn't work
let chArr = Array(chSet) // doesn't work
我稍微修改了解决方案,可以在@Larme和@vadian指出的答案中找到。两个答案最终都使用相同的算法。我只想看看这套内容。是的,想要这不是一件常见的事情。事实证明,获取CharacterSet
所有元素的唯一方法是遍历所有可能的 unicode 标量并检查它们是否属于该集合。对我来说感觉很奇怪,我们可以在Set
s,Array
s甚至Dictionaries
之间切换,如此容易。修改的原因是并尝试加快功能。我的粗略实验表明,即使我们最终创建一个字符串,使用标量的速度也快 30%。
extension CharacterSet {
func allUnicodeScalars() -> [UnicodeScalar] {
var result: [UnicodeScalar] = []
for plane in Unicode.UTF8.CodeUnit.min...16 where self.hasMember(inPlane: plane) {
for unicode in Unicode.UTF32.CodeUnit(plane) << 16 ..< Unicode.UTF32.CodeUnit(plane + 1) << 16 {
if let uniChar = UnicodeScalar(unicode), self.contains(uniChar) {
result.append(uniChar)
}
}
}
return result
}
}
// Testing and timing
printTimeElapsedWhenRunningCode(title:"allUnicodeScalars()") {
print(String.UnicodeScalarView(chSet.allUnicodeScalars()))
}
// Time elapsed for allUnicodeScalars(): 1.936843991279602 s.
printTimeElapsedWhenRunningCode(title:"allCharacters()") {
print(String(chSet.allCharacters()))
}
// Time elapsed for allCharacters(): 2.9846099615097046 s.
//Timing functions (for reference):
private func printTimeElapsedWhenRunningCode(title:String, operation:()->()) {
let startTime = CFAbsoluteTimeGetCurrent()
operation()
let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
print("Time elapsed for (title): (timeElapsed) s.")
}
private func timeElapsedInSecondsWhenRunningCode(operation: ()->()) -> Double {
let startTime = CFAbsoluteTimeGetCurrent()
operation()
let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
return Double(timeElapsed)
}
UPD:是的,这个问题是重复的,并且存在更好的答案。