问题
my problem is that i have an Array that holds some double values
NSArray *level4results = [context executeFetchRequest:request error:&error];
then i sum up all the values in that array
NSNumber *l4sum = [level4results valueForKeyPath:@"sum.self"];
The next thing i want to do is to divide by 8 the sum of the array... and this is where i am stuck. I have tried many options and ways of doing it by either way i kept on getting different error. This is what i have currently in my code
double l4average = ([l4sum doubleValue] / 8);
however this is throwing following error
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI doubleValue]: unrecognized selector sent to instance
Help in solving this problem appreciated. Thanks
回答1:
It's the collection operator. This line:
NSNumber *l4sum = [level4results valueForKeyPath:@"sum.self"];
Should be written:
NSNumber *l4sum = [level4results valueForKeyPath:@"@sum.self"];
回答2:
The sum operator needs an @
:
@"@sum.self"
来源:https://stackoverflow.com/questions/16630869/unable-to-change-nsnumber-to-double-and-do-some-calculations-with-it