无法将NSNumber更改为Double,并使用它进行一些计算 [英] Unable to change NSNumber to Double and do some calculations with it

查看:712
本文介绍了无法将NSNumber更改为Double,并使用它进行一些计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我有一个数组保存一些双重值

  NSArray * level4results = [context executeFetchRequest :请求错误:& error]; 

然后我总结该数组中的所有值

  NSNumber * l4sum = [level4results valueForKeyPath:@sum.self]; 

接下来我想做的是将数组的总和除以8这是我被卡住的地方。我尝试了许多选项和方式,通过我不断得到不同的错误。这是我现在在我的代码中

  double l4average =([l4sum doubleValue] / 8); 

但是这是抛出以下错误



原因:' - [__ NSArrayI doubleValue]:
无法识别的选择器发送到实例


帮助解决这个问题赞赏。谢谢

解决方案

这是集合操作符。这行:


NSNumber * l4sum = [level4results valueForKeyPath:@sum.self];


应写入:



NSNumber * l4sum = [level4results valueForKeyPath:@@ sum.self];


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

解决方案

It's the collection operator. This line:

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

Should be written:

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

这篇关于无法将NSNumber更改为Double,并使用它进行一些计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆