Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将NSMUTABLEARRY(NString)的元素转换为NSNumber并相加?_Ios_Objective C_Cocoa Touch_Nsarray_Nsnumber - Fatal编程技术网

Ios 如何将NSMUTABLEARRY(NString)的元素转换为NSNumber并相加?

Ios 如何将NSMUTABLEARRY(NString)的元素转换为NSNumber并相加?,ios,objective-c,cocoa-touch,nsarray,nsnumber,Ios,Objective C,Cocoa Touch,Nsarray,Nsnumber,好的,我有一个NsMutable数组,字符串的格式如下: 32,45,54,5550 etc. 将所有这些字符串转换为nsnumber并获取整个数组的和的最简单方法是什么 NSArray *array = @[ @"1,2", @"3,4" ]; NSUInteger sum = 0; for (NSString *str in array) { for (NSString *num in [str componentsSeparatedByString:@","]) {

好的,我有一个NsMutable数组,字符串的格式如下:

  32,45,54,5550 etc.
将所有这些字符串转换为nsnumber并获取整个数组的和的最简单方法是什么

NSArray *array = @[ @"1,2", @"3,4" ];
NSUInteger sum = 0;
for (NSString *str in array)
{
    for (NSString *num in [str componentsSeparatedByString:@","])
    {
        sum += [num intValue];
    }
}


您可以使用KVC collection运算符获取存储在数组中的所有数字字符串的总和

NSArray *numberStrings = @[@"32",@"45",@"54",@"5550"];
NSNumber* sum = [numberStrings valueForKeyPath: @"@sum.self"];
NSLog(@"%@", sum);
输出:
5681

对于简单字符串,它是有效的。但要注意:本地化可能存在陷阱。在那里,我仍然建议使用一个来创建真正的NSNumber对象,将它们存储在另一个数组中,并在上面使用
@sum


感谢布莱恩·陈让我参加考试


为了提高对本地化中可能存在的问题的认识,我想与大家分享这个实验:

在德语中,我们使用
分隔十进制数字,其中使用
将长数字分组为数千

因此,如果numberString数组可能由德语格式的数字填充,它可能看起来像
@[@“32,5”、@“45”、@“54”、@“5.550,00”]
。这个总数是
5681.5
,但是如果我们不修改代码,它不会失败,但更糟糕的是——计算错误:
136.55
。它只是忽略了逗号以外的所有内容,并将
视为十进制分隔符

让我们使用NSNumberFormatter来修复它:

我的系统设置为德语区域设置

NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
nf.locale = [NSLocale currentLocale];
[nf setGroupingSize:3];
nf.usesGroupingSeparator= YES;

NSArray *numberStrings = @[@"32,5", @"45", @"54", @"5.550,00"];
NSMutableArray *numbers = [@[] mutableCopy];

for (NSString *s in numberStrings) {
    NSNumber *n = [nf numberFromString:s];
    [numbers addObject:n];
}


NSNumber* sum = [numbers valueForKeyPath: @"@sum.self"];

NSLog(@"%@", sum);

它正确打印
5681.5

在数组上循环,将每个字符串转换为一个数值(可能是一个int)并将它们相加。@Zaph我不清楚字符串是数字还是类似于
@“1,2,3”
,但它也可以工作是的,假设最简单的事情。“Occam的剃须刀”会起作用吗?
NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
nf.locale = [NSLocale currentLocale];
[nf setGroupingSize:3];
nf.usesGroupingSeparator= YES;

NSArray *numberStrings = @[@"32,5", @"45", @"54", @"5.550,00"];
NSMutableArray *numbers = [@[] mutableCopy];

for (NSString *s in numberStrings) {
    NSNumber *n = [nf numberFromString:s];
    [numbers addObject:n];
}


NSNumber* sum = [numbers valueForKeyPath: @"@sum.self"];

NSLog(@"%@", sum);