Sorry for asking this, maybe is too stupid. I was searching a lot and couldn't find a way to do it. I have 2 NSArrays. The first one is a little tricky and could have 2 or more prices in each object, prices are separated by a comma inside object. The second one is simpler and has one price assigned in each individual object.
My goal is to sum all the numbers inside each Array and get the total amount of both.
I was dealing with two problems, first one is the fact that each object has a "$" and the other one was mentioned above. Is the fact that firstArray was builded in a way which makes more difficult to work with.
NSArray * myFirstArray = ["$ 22, $ 0", "$ 33, $ 666, $ 66, $ 22", "$ 123, $ 123, $ 555"];
NSArray * mySecondArray = ["$ 120", "$ 22", "$ 20", "$ 10", "$ 11", "$ 0", "$ 1234", "$ 3333"];
Hope this may help you!
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);
Similarly for 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);
console:
myFirstArray :: 1610
mySecondArray :: 4750
Sum :: 6360