I want to make method "additionMulti" that take n paramiters and add them in sum
My Code :
-(void) additionMulti:(NSArray *)data
{
int sum=0;
int xx;
for(xx=0; xx < [data count]; xx++)
{
sum =sum + [data objectAtIndex:xx];
}
NSLog(@"The result of adding %@ is : %i",data,sum);
}
If you need to calculate the sum from an array of objects, try with this:
- (void)additionMulti:(NSArray *)data {
int sum = 0;
int xx;
for(xx=0; xx<[data count]; xx++) {
sum = sum + [[data objectAtIndex:xx] intValue];
}
NSLog(@"The result of adding %@ is : %i",data,sum);
}
In order to let this code works, your array must be an array of NSNumber or NSString (so you can take the intValue from each value in the array).
In case you need to add an array to an already existing array, I suggest you to follow Julian' solution.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments