How on earth CharacterSet is not a collection?

Hi. I was intended to combine a `characterset.alphanumerics` and characterset.controlsymbols so I tried to use the most relevant method : for in traversal, but unfortunately, this didn't work because for some reason character set is not a collection! why?!

Accepted Answer

Basically, Swift.Collection is made of two essential features:

- provode iterator, where `next()` operation takes O(1) complexity

(This feature is represented by Sequence. To use for-in, you just need Sequence.)

- provide subscript-based access, which should also be done in O(1)


Currently, CharacterSet is a thin wrapper of ObjC class NSCharacterSet (which is based on a Core Foundation object CFCharacterSet),

and it is hard to provide two features above in O(1) complexity under the current implementation. (They were implemented far before Swift.Collection is introduced.)


In a future, there may be a better implementation to make CharacterSet conform to Collection, but until then, we cannot use CharacterSet as Collection. (In fact, CharacterSet is a set of Unicode Scalars, and it will be renamed...)


I do not understand what you mean by combine, but CharacterSet comforms to SetAlgebra and implements all operations defined in it.

You can write something like this.

let alnumControl = CharacterSet.alphanumerics.union( .controlCharacters)

I found this worth reading:

h ttps://nshipster.com/characterset/

As OOPer pointed out:

CharacterSet (and its reference type counterpart, NSCharacterSet) is a Foundation type used to trim, filter, and search for characters in text.

In Swift, a Character is an extended grapheme cluster (really just a String with a length of 1) that comprises one or more scalar values. CharacterSet stores those underlying Unicode.Scalar values, rather than Character values, as the name might imply.

The “set” part of CharacterSet refers not to Set from the Swift standard library, but instead to the SetAlgebra protocol, which bestows the type with the same interface: contains(_:), insert(_:), union(_:), intersection(_:), and so on.


And if you want to loop through it nevertheless:

https://stackoverflow.com/questions/55473051/converting-a-characterset-to-a-string-or-to-an-array-or-set-of-characters

How on earth CharacterSet is not a collection?
 
 
Q