有没有一种简单的方法来以编程方式获取字母表?

发布于 2024-10-06 03:45:51 字数 154 浏览 0 评论 0原文

我想要一个包含字母表中所有字母的 NSArray/NSMutableArray 。一定有一种快速简单的方法,比把它们全部敲出来更好。以 PHP 为例:

foreach(range('A','Z') as $i) $alphabet[]=$i;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

简单 2024-10-13 03:45:51

还可以使用为表索引标题生成的数组。它不使用 for 循环并且具有多语言支持。

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

//Remove the last object (extra), '#' from the array.
[alphabets removeLastObject];

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];
踏雪无痕 2024-10-13 03:45:51

没有比把它们全部打印出来更快的方法了,除非你从下面剪切并粘贴我方便的参考!

“abcdefghijklmnopqrstuvwxyz”


为此,这里有一个更长的方法。

for (char a = 'a'; a <= 'z'; a++)
{
  [myArray addObject:[NSString stringWithFormat:@"%c", a]];
}

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]];
}
偷得浮生 2024-10-13 03:45:51

有时,输入字母是最简单的。这里它们是一个数组:

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:@" "];

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:@" "];
森末i 2024-10-13 03:45:51

尝试使用以下代码;


int a = 65;
for (; a < 91; a++) {
    [array addObject:[NSString stringWithFormat:@"%c", (char)a]];
}
NSLog(@"%@", array);

try with following code;


int a = 65;
for (; a < 91; a++) {
    [array addObject:[NSString stringWithFormat:@"%c", (char)a]];
}
NSLog(@"%@", array);
歌入人心 2024-10-13 03:45:51

您可以使用 for 循环来生成它们,但我认为将它们输入出来更容易。这肯定比在这里发布问题更容易。 ;)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文