NSArray 的前三项转换为 NSString?

发布于 2024-11-30 17:17:26 字数 458 浏览 1 评论 0原文

将数组的前三个对象(或者 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 技术交流群。

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

发布评论

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

评论(4

梦在深巷 2024-12-07 17:17:26

您可以使用 subarrayWithRange: 来实现:

NSString *res = [[[theArray subarrayWithRange:NSMakeRange(0, fmin(3, [theArray count]))] 
                  valueForKey:@"brandName"] 
                 componentsJoinedByString:@", "];

You can use subarrayWithRange: for that:

NSString *res = [[[theArray subarrayWithRange:NSMakeRange(0, fmin(3, [theArray count]))] 
                  valueForKey:@"brandName"] 
                 componentsJoinedByString:@", "];
旧城空念 2024-12-07 17:17:26
NSUInteger count = [bandArray count];
if (count > 3){
    count = 3;
}
NSString * resultString = [[bandArray subarrayWithRange:NSMakeRange(0,count)]  componentsJoinedByString:@", "];
NSUInteger count = [bandArray count];
if (count > 3){
    count = 3;
}
NSString * resultString = [[bandArray subarrayWithRange:NSMakeRange(0,count)]  componentsJoinedByString:@", "];
请别遗忘我 2024-12-07 17:17:26

您可以尝试以下操作(尽管它完全未经测试,因为我远离我的 Mac

int bandCount = 1;
NSString *bands;
for (NSString *band in bandArray) {
    if (bandCount > 3) break;
    if (bandCount == 1) {
        bands = [NSString stringWithFormat:@"%@", band];
    } else {        
        bands = [NSString stringWithFormat:@"%@, %@", bands, band];
    }
    bandCount ++;
}

You could try the following (although it's completely untested as I'm away from my Mac)

int bandCount = 1;
NSString *bands;
for (NSString *band in bandArray) {
    if (bandCount > 3) break;
    if (bandCount == 1) {
        bands = [NSString stringWithFormat:@"%@", band];
    } else {        
        bands = [NSString stringWithFormat:@"%@, %@", bands, band];
    }
    bandCount ++;
}
不语却知心 2024-12-07 17:17:26

可能不是最快的,但却是最简单的,谁知道苹果苹果在创建子数组时可能会做一些聪明的事情。

[[array subarrayWithRange:NSMakeRange(0,MIN(array.length,3)] componentsJoinedByString:@","];

May not be the quickest but the simplest, and who know apple apple may do some smarts when creating subarrays.

[[array subarrayWithRange:NSMakeRange(0,MIN(array.length,3)] componentsJoinedByString:@","];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文