Unable to change NSNumber to Double and do some calculations with it

女生的网名这么多〃 提交于 2019-12-23 05:55:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!