10

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;
Cœur
  • 37,241
  • 25
  • 195
  • 267
Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224

5 Answers5

24

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];
Abdurrahman Mubeen Ali
  • 1,331
  • 1
  • 13
  • 19
23

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]];
}
Abdurrahman Mubeen Ali
  • 1,331
  • 1
  • 13
  • 19
Alex Brown
  • 41,819
  • 10
  • 94
  • 108
8

Sometimes typing the letters out is the easiest. Here they are as an array:

NSArray *letters = [@"A B C D E F G H I J K L M N O P Q R S T U V W X Y Z" componentsSeparatedByString:@" "];
Abdurrahman Mubeen Ali
  • 1,331
  • 1
  • 13
  • 19
Denis Hennessy
  • 7,243
  • 4
  • 25
  • 31
  • that's a novel and useful way of doing it. however the point is it takes time to type out each letter of the alphabet. The keys aren't in the best places, and its much more natural to type out a for loop on the fly, than type the alphabet (I find anyway). – Thomas Clayson Dec 07 '10 at 15:20
  • I was thinking about this, but having to add the spaces in just annoys me. also, manually typing all 26 letters means you might make a mistake. :-p – Alex Brown Dec 07 '10 at 15:22
  • to be honest, my first instinct was to use a loop. this is definitely an area where languages like ruby are better suited. – Denis Hennessy Dec 07 '10 at 15:31
  • the other slight benefit is that it's easier to accommodate non-contiguous ranges (like adding 0-9) – Denis Hennessy Dec 07 '10 at 15:31
  • 1
    And in iOS, if you sneak in {search} as the first word in that alpha-string, you will get the magnifying glass as the top character of the index. And if you add # as the last character, to indicate all numeric entries, you get an identical index to that used by the Apple Music app. – Johan Nov 05 '13 at 08:36
  • The best solution doesn't always have to be the most clever solution. – tarrball Jan 18 '18 at 16:42
4

try with following code;


int a = 65;
for (; a < 91; a++) {
    [array addObject:[NSString stringWithFormat:@"%c", (char)a]];
}
NSLog(@"%@", array);
jfalexvijay
  • 3,681
  • 7
  • 42
  • 68
0

You could use a for-loop to generate them, but I think typing them out is easier. It is most certainly easier than posting a question here. ;)

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • thats what I want, the for loop, but I don't know what to do. See with php you can do a simple for loop... – Thomas Clayson Dec 07 '10 at 15:02
  • Yes, well, I don't know much about iPhone development, but NSArray seems to be unmutable. That suggests you won't be able to add items using a for loop. You can use its descendent: NSMutableArray, or you should declare the array at once. – GolezTrol Dec 07 '10 at 15:14
  • You can take a look here: http://stackoverflow.com/questions/908742/nsdictionary-objective-c – GolezTrol Dec 07 '10 at 15:16
  • yeah, sorry GolezTrol - I needed the output to be NSArray, but I can just set it to NSArray using arrayWithArray:mutableArray so, it doesn't matter - I have nothing against using a Mutable Array :) – Thomas Clayson Dec 07 '10 at 15:19