Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an easy way to programmatically get the alphabet?

I want an NSArray/NSMutableArray containing all the letters of the alphabet. There must be a quick and easy way, better than typing them all out. For example in PHP:

foreach(range('A','Z') as $i) $alphabet[]=$i;
like image 612
Thomas Clayson Avatar asked Dec 07 '10 14:12

Thomas Clayson


2 Answers

The array generated for table index titles may also be used. It does not use a for loop and has multi-language support.

NSMutableArray *alphabets = [[NSMutableArray alloc] initWithArray:[[UILocalizedIndexedCollation currentCollation] sectionIndexTitles]];

//Remove the last object (extra), '#' from the array.
[alphabets removeLastObject];
like image 130
Abdurrahman Mubeen Ali Avatar answered Oct 12 '22 02:10

Abdurrahman Mubeen Ali


There's no quicker way than typing them all out, unless you cut and paste my handy reference from below!

"abcdefghijklmnopqrstuvwxyz"


For the sake of it, here's a longer way.

for (char a = 'a'; a <= 'z'; a++)
{
  [myArray addObject:[NSString stringWithFormat:@"%c", a]];
}
like image 32
Alex Brown Avatar answered Oct 12 '22 02:10

Alex Brown