NSArray 的前三项转换为 NSString?
将数组的前三个对象(或者 1 或 2,如果数组有多大)转换为以逗号分隔的字符串的最有效方法是什么。我有一种感觉,有一种方法可以用块来实现,但我无法解决。
对象是 Bands,存储在 bandArray 中,每个 band 的属性包括 bandName。
所以输出会是这样的
String
"Abba" <- when there is one object
"Abba, Kiss" <- when there is two objects
"Abba, Kiss, Nirvana" <- when there is three objects
"Abba, Kiss, Nirvana" <- when there is four objects. after three, names are ignored
What would be the most efficient way of turning the first three objects (or 1 or 2 if that's how big the array is) of an array, into a string, which is comma-separated. I've got a feeling there is a way to this with blocks, but I can't work it out
The objects are Bands, stored in bandArray, and the attributes of each band include a bandName.
So the output would be something like
String
"Abba" <- when there is one object
"Abba, Kiss" <- when there is two objects
"Abba, Kiss, Nirvana" <- when there is three objects
"Abba, Kiss, Nirvana" <- when there is four objects. after three, names are ignored
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
subarrayWithRange:
来实现:You can use
subarrayWithRange:
for that:您可以尝试以下操作(尽管它完全未经测试,因为我远离我的 Mac)
You could try the following (although it's completely untested as I'm away from my Mac)
可能不是最快的,但却是最简单的,谁知道苹果苹果在创建子数组时可能会做一些聪明的事情。
May not be the quickest but the simplest, and who know apple apple may do some smarts when creating subarrays.