float INFINITY可以由NSCoder存档,但不能取消存档 [英] float INFINITY can be archived by NSCoder, but not dearchived
问题描述
我必须将值INFINITY
的float
存档,然后对其进行解存档.
这是我的示例代码:
I have to archive a float
with value INFINITY
, and later to dearchive it.
Here is my example code:
要归档的对象:
@interface CodeInf : NSObject <NSCoding>
@end
@implementation CodeInf
- (void)encodeWithCoder:(NSCoder *)encoder {
float inf = INFINITY;
[encoder encodeFloat: inf forKey:@"INFINITY"];
}
- (id)initWithCoder:(NSCoder *)decoder {
if (self = [super init]) {
float decodedInf = [decoder decodeFloatForKey: @"INFINITY"];
}
return self;
}
@end
这是归档/归档代码:
CodeInf *myCodeInf = [[CodeInf alloc] init];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:myCodeInf];
myCodeInf = [NSKeyedUnarchiver unarchiveObjectWithData:data];
归档可以工作,但是归档会引发错误:
Archiving works, but dearchiving raises the error:
*由于未捕获的异常'NSRangeException'而终止应用程序,原因:'* -[NSKeyedUnarchiver encodeFloatForKey:]:的值(inf)为 键(INFINITY)太大,无法放入32位浮点数"
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSKeyedUnarchiver decodeFloatForKey:]: value (inf) for key (INFINITY) too large to fit in 32-bit float'
这是解压缩程序中的错误,还是我出了什么问题?
Is this a bug in the dearchiver, or do I something wrong?
推荐答案
看起来像个错误.向苹果提交错误报告.
Looks like a bug. Submit a bug report to Apple.
作为解决方法,请使用encodeDouble
和decodeDoubleForKey
-您可以将值保留为float
,并且C规则不需要强制转换.
As a workaround use encodeDouble
and decodeDoubleForKey
- you can keep your value as a float
and no casts are needed by C rules.
这篇关于float INFINITY可以由NSCoder存档,但不能取消存档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!