挑战:如何将 NSArray 的数字转换为英语可读的列表

发布于 2024-09-13 22:56:23 字数 497 浏览 3 评论 0原文

我正在寻找一个漂亮的干净例程,用于将包含 NSNumbers (整数)的 NSArray 转换为漂亮的英文可读字符串。例如,我想将以下内容更改

[NSArray arrayWithObjects:[NSNumber numberWithInt:5],
                          [NSNumber numberWithInt:7],
                          [NSNumber numberWithInt:12],
                          [NSNumber numberWithInt:33], nil];

为:

5、7、12 和 13。

有没有什么好的方法可以在没有可怕的 if 语句逻辑的情况下做到这一点?我确信可以使用正则表达式来完成,但这在 iOS 4 之前的代码中可能吗?

谢谢! :)

:-乔

I'm looking for a nice clean routine for turning an NSArray containing NSNumbers (integers) into a nice English-readable string. For example, I want to change this:

[NSArray arrayWithObjects:[NSNumber numberWithInt:5],
                          [NSNumber numberWithInt:7],
                          [NSNumber numberWithInt:12],
                          [NSNumber numberWithInt:33], nil];

into this:

5, 7, 12 and 13.

Is there any nice way of doing this without hideous if statement logic? I'm sure it can be done with a regular expression, but is that possible in pre-iOS 4 code?

Thanks! :)

:-Joe

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

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

发布评论

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

评论(2

苦笑流年记忆 2024-09-20 22:56:23
NSArray *numbers = [NSArray arrayWithObjects:[NSNumber numberWithInt:5],
                          [NSNumber numberWithInt:7],
                          [NSNumber numberWithInt:12],
                          [NSNumber numberWithInt:33], nil];
NSArray *first = [numbers subarrayWithRange:NSMakeRange(0, [numbers count]-1)];
NSString *joined = [[first componentsJoinedByString:@", "] stringByAppendingFormat:(([first count] > 0) ? @"and %@" : @"%@"), [numbers lastObject]];
NSArray *numbers = [NSArray arrayWithObjects:[NSNumber numberWithInt:5],
                          [NSNumber numberWithInt:7],
                          [NSNumber numberWithInt:12],
                          [NSNumber numberWithInt:33], nil];
NSArray *first = [numbers subarrayWithRange:NSMakeRange(0, [numbers count]-1)];
NSString *joined = [[first componentsJoinedByString:@", "] stringByAppendingFormat:(([first count] > 0) ? @"and %@" : @"%@"), [numbers lastObject]];
新一帅帅 2024-09-20 22:56:23

妈妈看,没有如果!

NSString *seps[] = {@" and ",@"",@", ",nil,nil,@", "}, *o = @".", *sep = o;
for ( NSNumber *n in [arr reverseObjectEnumerator] ) {
   o = [NSString stringWithFormat:@"%@%@%@",n,sep = seps[[sep length]],o];
}

NSLog(@"out: %@",o);

输出:

out: 5, 7, 12 and 33.

但是为什么呢?

编辑 这是一个可以理解的版本,没有“巨大的 if/else 构造”

NSString *out = @"";  // no numbers = empty string
NSString *sep = @"."; // the separator first used is actually the ending "."
for ( NSNumber *n in [arr reverseObjectEnumerator] )
{
    out = [NSString stringWithFormat:@"%@%@%@",n,sep,out];
    if ( [sep isEqualToString:@"."] ) // was the separator the ending "." ?
    {
        sep = @" and "; // if so, put in the last separator " and "
    } else {
        sep = @", ";    // otherwise, use separator ", "
    }
}

这将输出类似的字符串

0 elements: ""        (i.e. empty)
1 element:  "33."
2 elements: "12 and 33."
3 elements: "7, 12 and 33."

Look mom, no ifs!

NSString *seps[] = {@" and ",@"",@", ",nil,nil,@", "}, *o = @".", *sep = o;
for ( NSNumber *n in [arr reverseObjectEnumerator] ) {
   o = [NSString stringWithFormat:@"%@%@%@",n,sep = seps[[sep length]],o];
}

NSLog(@"out: %@",o);

output:

out: 5, 7, 12 and 33.

but why?

edit here is an understandable version with no "huge if/else construct"

NSString *out = @"";  // no numbers = empty string
NSString *sep = @"."; // the separator first used is actually the ending "."
for ( NSNumber *n in [arr reverseObjectEnumerator] )
{
    out = [NSString stringWithFormat:@"%@%@%@",n,sep,out];
    if ( [sep isEqualToString:@"."] ) // was the separator the ending "." ?
    {
        sep = @" and "; // if so, put in the last separator " and "
    } else {
        sep = @", ";    // otherwise, use separator ", "
    }
}

This will output strings like

0 elements: ""        (i.e. empty)
1 element:  "33."
2 elements: "12 and 33."
3 elements: "7, 12 and 33."
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文