Fondation has CharacterSet struct (briged to NSCharacterSet) for managing sets of characters, for example when working with Formatter instances. Surprisingly, CharacterSet is not a Set, although the functionality and purpose is totally the same. Unfortunately CharacterSet is not a Collection ether, so right now I have no idea how to retrieve its elements.
// 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
I've slightly modified the solution, found in answers pointed out by @Larme and @vadian. Both answers end up with the same algorithm. All I wanted was to look at the contents of the set. Yes, it is not a common thing to want to. It turns out that the only way to get all the elements of
CharacterSetis to loop through all possible unicode scalars and check if they belong to the set. Feels so strange to me in the word where we can switch betweenSets,Arrays and evenDictionariesso easy. The reason for modification is and attempt to speed up the function. My rough experiments show that using scalars is 30% faster even if we create a string in the end.UPD: Yes, the question is a duplicate, and a better answer exists.