0

i having a scenario get product/multiply of array integers. were i'm to able for sum using below line of code

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

Using NSExpression also able to get sum but not getting product/Multiply

NSExpression *expression = [NSExpression expressionForFunction:@"sum:" arguments:@[[NSExpression expressionForConstantValue:intarr]]];  
id result = [expression expressionValueWithObject:nil context:nil];

can anyone suggest me how to get product/multiply of self Array of integer

1 Answers1

0

As per Greg's answer If you don't want to use loop then use:

NSArray *nums = @[@1, @2, @3, @4, @5];
NSExpression *expression = [NSExpression expressionForFunction:@"sum:" arguments:@[[NSExpression expressionForConstantValue:nums]]];
id result = [expression expressionValueWithObject:nil context:nil];
swapnil patel
  • 384
  • 2
  • 17
  • im trying to get Product/Multiplication Not sum – Mohnish Vardhan Mar 28 '17 at 08:02
  • @MohnishVardhan You could use `multiply:by:` instead of `sum:`, but I guess (haven't tested) that this only works with 2 variables. Anyway, you can try. Here is an example of it: http://stackoverflow.com/a/29517108/4370893 – VitorMM Mar 29 '17 at 12:02