开发者

Is there a built in way of getting arrays of common sets like alphabet, numbers, etc in ios?

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

NSArray *alphabet = [So开发者_高级运维meClass lowerCaseAlphabet];

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


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.


NSMutableCharacterSet *cset = [NSMutableCharacterSet lowercaseLetterCharacterSet];

I don't know if this is localizable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜