[swift-users] CharacterSet vs Set<Character>

Dave Abrahams dabrahams at apple.com
Sun Oct 2 13:56:19 CDT 2016


on Sun Oct 02 2016, Jean-Denis Muys <swift-users-AT-swift.org> wrote:

> I was playing with CharacterSet, and I came up with:
>
> let vowels = CharacterSet(charactersIn: "AEIOU")

Yeah, because CharacterSet is a set of UnicodeScalars, not a set of
Character.  That should probably get fixed somehow.  I suggest filing a
radar against Foundation.

> let char: Character = "E"
>
> vowels.contains(char)
>
> That last line doesn't compile: I get "*cannot convert value of type
> 'Character' to expected argument type 'UnicodeScalar'*"
>
> The problem is, I could not find a simple way to convert from a character
> to a unicodeScalar. The best I found is the very ugly:
>
> vowels.contains(String(char).unicodeScalars[String(char).unicodeScalars.
> startIndex])
>
> Did I miss anything? 

  vowels.contains(String(char).unicodeScalars.first!)

> Does it have to be that horrific?

For now, I'm afraid I don't have anything better for you.  I very much
hope to improve String usability substantially for Swift 4.

> If so, I find using Set much better:
>
> let vowelsSet: Set<Character> = Set("AEIOU".characters)
>
> vowelsSet.contains(char)
>
> I must have missed something. Any suggestion welcome

-- 
-Dave



More information about the swift-users mailing list