包含价格的NSStrings的Sum数组

时间:2016-05-31 05:47:24

标签: ios objective-c nsstring nsarray

很抱歉这个问题,也许太愚蠢了。我经常搜索,找不到办法。我有2个NSArrays。第一个有点棘手,每个对象可能有2个或更多的价格,价格由对象内的逗号分隔。第二个更简单,每个对象分配一个价格。

我的目标是将每个数组中的所有数字相加并获得两者的总数。

我正在处理两个问题,第一个问题是每个对象都有一个“$”,另一个是上面提到的。事实上,firstArray的构建方式更难以使用。

NSArray * myFirstArray = ["$ 22, $ 0", "$ 33, $ 666, $ 66, $ 22", "$ 123, $ 123, $ 555"];

NSArray * mySecondArray = ["$ 120", "$ 22", "$ 20", "$ 10", "$ 11", "$ 0", "$ 1234", "$ 3333"];

5 个答案:

答案 0 :(得分:2)

你的阵列的定义似乎很乱。它来自JSON吗?

如果是:

-(NSInteger)sumJSONArrayStr:(NSString *)jsonStr
{
    NSArray *array = [NSJSONSerialization JSONObjectWithData:[jsonStr dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
    NSMutableArray *cleanAndFlatenArray = [[NSMutableArray alloc] init];
    NSMutableCharacterSet *charSet = [NSMutableCharacterSet characterSetWithCharactersInString:@"$"];
    [charSet formUnionWithCharacterSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    for (NSString *aStr in array)
    {
        NSArray *split = [aStr componentsSeparatedByString:@","];
        for (NSString *aSplit in split)
        {
            [cleanAndFlatenArray addObject:[aSplit stringByTrimmingCharactersInSet:charSet]];
        }
    }
    NSNumber *sum = [cleanAndFlatenArray valueForKeyPath:@"@sum.integerValue"];
    return [sum integerValue];
}

呼叫:

NSInteger val1 = [self sumJSONArrayStr:@"[\"$ 22, $ 0\", \"$ 33, $ 666, $ 66, $ 22\", \"$ 123, $ 123, $ 555\"]"];
NSInteger val2 = [self sumJSONArrayStr:@"[\"$ 120\", \"$ 22\", \"$ 20\", \"$ 10\", \"$ 11\", \"$ 0\", \"$ 1234\", \"$ 3333\"]"];

如果没有,只需考虑展平方法和删除“$”字符。

答案 1 :(得分:1)

希望这可以帮到你!

  NSInteger ir = 0;
  NSArray * myFirstArray =[[NSMutableArray alloc]initWithObjects:@"$ 22, $ 0", @"$ 33, $ 666, $ 66, $ 22", @"$ 123, $ 123, $ 555", nil];

   for (int i =0; i<myFirstArray.count; i++) {
    NSArray* foo = [[myFirstArray objectAtIndex:i] componentsSeparatedByString: @","];
    NSLog(@"foo %@",foo);
    for (int j =0; j<foo.count; j++) {
        NSString *rest = [NSString stringWithFormat:@"%@",[foo objectAtIndex:j]];
        NSString* noSpaces =  [rest stringByReplacingOccurrencesOfString:@"$" withString:@""];
        ir = ir +[noSpaces integerValue];
    }
}

NSLog(@"myFirstArray :: %ld",(long)ir);

同样适用于secondArray:

  NSInteger ir1 = 0;
  NSArray * mySecondArray =[[NSMutableArray alloc]initWithObjects:@"$ 120", @"$ 22", @"$ 20", @"$ 10", @"$ 11", @"$ 0", @"$ 1234", @"$ 3333", nil];

  for (int k =0; k<mySecondArray.count; k++) {
             NSString *rest = [NSString stringWithFormat:@"%@",[mySecondArray objectAtIndex:k]];
        NSString* noSpaces =  [rest stringByReplacingOccurrencesOfString:@"$" withString:@""];
        ir1 = ir1 +[noSpaces integerValue];
 }


 NSInteger    Sum = ir + ir1 ;
 NSLog(@"myFirstArray :: %ld",(long)ir);
 NSLog(@"mySecondArray :: %ld",(long)ir1);
 NSLog(@"Sum :: %ld",(long)Sum);
  

控制台:

     

myFirstArray :: 1610

     

mySecondArray :: 4750

     

Sum :: 6360

答案 2 :(得分:0)

如果要对Array中的所有数字求和,首先使用stringByReplacingOccurrencesOfString删除所有空格,逗号和$。然后你可以对数组中的所有数字求和。

用于以下代码:

NSInteger sum = 0;
for (NSNumber *num in myFirstArray)

{

  sum += [num intValue];
}

对第二个阵列做同样的事情:

    NSInteger sum = 0;
for (NSNumber *num in mySecondArray)

{

  sum += [num intValue];
}

答案 3 :(得分:0)

如果您的数组只包含空格,逗号和&#39; $&#39;字符使用以下代码作为第一个数组

int totalPrice = 0;
NSString *tempPrice;
NSArray *tempArray;
for (NSString *multiplePriceString in myFirstArray) {
    tempPrice = [multiplePriceString stringByReplacingOccurrencesOfString:@" " withString:@""];
    tempPrice = [multiplePriceString stringByReplacingOccurrencesOfString:@"$" withString:@""];
    tempArray = [tempPrice componentsSeparatedByString:@","];
    for (NSString *price in tempArray) {
        totalPrice = totalPrice + [price intValue];
    }
}

对于第二阵列使用

int totalPrice = 0;
NSString *tempPrice;
for (NSString *singlePriceString in myFirstArray) {
    tempPrice = [singlePriceString stringByReplacingOccurrencesOfString:@" " withString:@""];
    tempPrice = [singlePriceString stringByReplacingOccurrencesOfString:@"$" withString:@""];
    totalPrice = totalPrice + [singlePriceString intValue];
}

答案 4 :(得分:0)

此代码可以帮助您:

saveStudents: function() {
   return Ember.RSVP.all(this.get('model.students').invoke('save'))
}