本文介绍了{NSDecimalNumber integerValue}在iOS8中表现奇怪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的团队,这很奇怪。 [NSDecimalNumber integerValue]表现得很奇怪。



我坐在一个断点处,试图弄清楚为什么我的应用程序的某些部分在iOS8中被破坏了,我是查看名为timeSeconds的变量。它在Xcode变量视图中显示如下:

  _timeSeconds(NSDecimalNumber *)344.514533996581994496 

但是当我在调试器中查询它时,我看到了:

 (lldb)p [self.timeSeconds doubleValue] 
(double)$ 14 = 344.51453399658192
(lldb)p [self.timeSeconds intValue]
(int)$ 15 = 344
(lldb)p [self.timeSeconds integerValue]
(NSInteger)$ 16 = -5
(lldb)p(NSInteger)[self.timeSeconds intValue]
(NSInteger)$ 17 = 344

看到-5?在我提交雷达之前,你们中的任何人都可以复制或解释吗?



这是SSCCE:

  NSDecimalNumber * n = [NSDecimalNumber decimalNumberWithString:@344.514533996581994496]; 
NSLog(@%@,n); // 344.514533996581994496
NSLog(@%ld,(long)[n intValue]); // 344
NSLog(@%ld,(long)[n integerValue]); // -5
NSLog(@%ld,(long)[n unsignedIntegerValue]); // 12

提前致谢!



Matthew

解决方案

integerValue 的结果令人惊讶,但据我所知,记录在案:



NSDecimalNumber 继承自 NSNumber 。在 NSNumber 的子类注释中,声明
...子类必须覆盖与声明的类型对应的访问器方法 - 例如,如果你的objCType实现返回i,你必须覆盖intValue ......



objCType 设置为内部指针,所以它应该是与 NSNumber 相同。



NSDecimal 不会覆盖 intergerValue
确实会覆盖 doubleValue ,所以应该可以正常工作。



唯一让我惊讶的是:它似乎没有覆盖 intValue ...


OK team, this is weird. [NSDecimalNumber integerValue] is behaving strangely.

I'm sat at a breakpoint, trying to figure out why some parts of my app are broken in iOS8, and I'm looking at a variable called "timeSeconds". It appears like this in the Xcode Variables View:

_timeSeconds    (NSDecimalNumber *) 344.514533996581994496

But when I query it in the debugger, I see this:

(lldb) p [self.timeSeconds doubleValue]
(double) $14 = 344.51453399658192
(lldb) p [self.timeSeconds intValue]
(int) $15 = 344
(lldb) p [self.timeSeconds integerValue]
(NSInteger) $16 = -5
(lldb) p (NSInteger)[self.timeSeconds intValue]
(NSInteger) $17 = 344

See that "-5"? Can any of you beautiful people reproduce or explain that, before I file a radar?

Here's the SSCCE:

NSDecimalNumber *n = [NSDecimalNumber decimalNumberWithString:@"344.514533996581994496"];
NSLog(@"%@", n); // 344.514533996581994496
NSLog(@"%ld", (long)[n intValue]); // 344
NSLog(@"%ld", (long)[n integerValue]); // -5
NSLog(@"%ld", (long)[n unsignedIntegerValue]); // 12

Thanks in advance!

Matthew

解决方案

The result for integerValue is surprising, but from what I understand as documented:

NSDecimalNumber inherits from NSNumber. In the subclassing notes from NSNumber it is stated that "... subclass must override the accessor method that corresponds to the declared type—for example, if your implementation of objCType returns "i", you must override intValue ..."

objCType is set to the inner pointer, so it should be the same as for NSNumber.

NSDecimal does not override intergerValue.It does override doubleValue, so that should work fine.

The only thing that makes me wonder: it seems it does not override intValue either ...

这篇关于{NSDecimalNumber integerValue}在iOS8中表现奇怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 12:57