You should not; this is not the purpose of a character set. A NSCharacterSet
is a possibly-infinite set of characters, possibly in not-yet-invented code points. All you want to know is "Is this character or collection of characters in this set?", and to that end it is useful.
Imagine this Swift code:
let asciiCodepoints = Unicode.Scalar(0x00)...Unicode.Scalar(0x7F)
let asciiCharacterSet = CharacterSet(charactersIn: asciiCodepoints)
let nonAsciiCharacterSet = asciiCharacterSet.inverted
Which is analogous to this Objective-C code:
NSRange asciiCodepoints = NSMakeRange(0x00, 0x7F);
NSCharacterSet * asciiCharacterSet = [NSCharacterSet characterSetWithRange:asciiCodepoints];
NSCharacterSet * nonAsciiCharacterSet = asciiCharacterSet.invertedSet;
It's easy to say "loop over all the characters in asciiCharacterSet
"; that would just loop over all characters from U+0000
through U+007F
. But what does it mean to loop over all the characters in nonAsciiCharacterSet
? Do you start at U+0080
? Who's to say there won't be negative codepoints in the future? Where do you end? Do you skip non-printable characters? What about extended grapheme clusters? Since it's a set (where order doesn't matter), can your code handle out-of-order codepoints in this loop?
These are questions you don't want to answer here; functionally nonAsciiCharacterSet
is infinite, and all you want to use it for is to tell if any given character lies outside the set of ASCII characters.
The question you should really be asking yourself is: "What do I want to accomplish with this array of capital letters?" If (and likely only if) you really need to iterate over it in order, putting the ones you care about into an Array
or String
(perhaps one read in from a resource file) is probably the best way. If you want to check to see if a character is part of the set of uppercase letters, then you don't care about order or even how many characters are in the set, and should use CharacterSet.uppercaseLetters.contains(foo)
(in Objective-C: [NSCharacterSet.uppercaseLetterCharacterSet contains: foo]
).
Think, too, about non-latin characters. CharacterSet.uppercaseLetters
covers Unicode General Categories Lu and Lt, which contain A
through Z
and also things like Dž
,
, and Խ
. You don't want to have to think about this. You definitely don't want to issue an update to your app when the Unicode Consortium adds new characters to this list. If what you want to do is decide whether something is upper-case, don't bother hard-coding anything.