Is there an easy way to programmatically get the alphabet?

前端 未结 5 1357
臣服心动
臣服心动 2021-02-04 12:11

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

相关标签:
5条回答
  • 2021-02-04 12:53

    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:@" "];
    
    0 讨论(0)
  • 2021-02-04 12:57

    try with following code;

    
    int a = 65;
    for (; a < 91; a++) {
        [array addObject:[NSString stringWithFormat:@"%c", (char)a]];
    }
    NSLog(@"%@", array);
    
    0 讨论(0)
  • 2021-02-04 13:02

    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];
    
    0 讨论(0)
  • 2021-02-04 13:09

    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. ;)

    0 讨论(0)
  • 2021-02-04 13:11

    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]];
    }
    
    0 讨论(0)
提交回复
热议问题