0

I would assume there would be a convenience class somewhere where I could call say

NSArray *alphabet = [SomeClass lowerCaseAlphabet];

instead of having to write it out every time. Is there such a class?

Marty
  • 5,926
  • 9
  • 53
  • 91

2 Answers2

0

Yes, a quick search in the docs even throws up some code using NSMutableCharacterSet:

addCharactersInRange: Adds to the receiver the characters whose Unicode values are in a given range.

- (void)addCharactersInRange:(NSRange)aRange

Parameters:

aRange: The range of characters to add. aRange.location is the value of the first character to add; aRange.location + aRange.length– 1 is the value of the last. If aRange.length is 0, this method has no effect.

Discussion

This code excerpt adds to a character set the lowercase English alphabetic characters:

NSMutableCharacterSet *aCharacterSet = [[NSMutableCharacterSet alloc] init];
NSRange lcEnglishRange;

lcEnglishRange.location = (unsigned int)'a';
lcEnglishRange.length = 26;
[aCharacterSet addCharactersInRange:lcEnglishRange];
//....
[aCharacterSet release];

Availability:

Available in iOS 2.0 and later.

/////////

Personal opinion: If you get stuck for a while on these things it's often just quicker to create something for yourself. In this case there's probably not much to lose by making an instance of NSArray with objects @"a", ..., @"z". An array of twenty-six or fifty-two characters is not very big.

Ken
  • 30,811
  • 34
  • 116
  • 155
  • True. I did end up doing that, just thought it would be convenient for future reference. And it looks a lot spiffier in the code ;) – Marty Feb 10 '11 at 00:23
  • There isn't much to loose unless you are trying to create a multi-lingual application -- and not all languages use the same letters. – Chris Brandsma Jun 19 '11 at 21:44
0

NSMutableCharacterSet *cset = [NSMutableCharacterSet lowercaseLetterCharacterSet];

I don't know if this is localizable.

Chris Brandsma
  • 11,666
  • 5
  • 47
  • 58