在ios中是否有一种内置的方法来获取常见集合(如字母、数字等)的数组?
我假设在某个地方会有一个方便的类,我可以在其中调用 say
NSArray *alphabet = [SomeClass lowerCaseAlphabet];
而不必每次都写出来。有这样的班级吗?
I would assume there would be a convenience class somewhere where I could call say
NSArray *alphabet = [SomeClass lowerCaseAlphabet];
instead of having to write it out every time. Is there such a class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,在文档中快速搜索甚至会抛出一些使用 NSMutableCharacterSet 的代码:
addCharactersInRange:
将 Unicode 值在给定范围内的字符添加到接收者。
参数:
a范围:
要添加的字符范围。
aRange.location 是要添加的第一个字符的值; aRange.location + aRange.length – 1 是最后一个的值。如果 aRange.length 为 0,则此方法无效。
讨论
此代码摘录将小写英文字母字符添加到字符集中:
可用性:
在 iOS 2.0 及更高版本中可用。
/////////
个人观点:如果你在这些事情上卡住了一段时间,那么为自己创建一些东西通常会更快。在这种情况下,使用对象 @"a", ..., @"z" 创建 NSArray 实例可能不会造成太大损失。二十六或五十二个字符的数组并不是很大。
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.
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:
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];
我不知道这是否可以本地化。
NSMutableCharacterSet *cset = [NSMutableCharacterSet lowercaseLetterCharacterSet];
I don't know if this is localizable.