我必须float
用 value归档 a INFINITY
,然后再去归档它。
这是我的示例代码:
要归档的对象:
@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];
归档有效,但取消归档会引发错误:
* 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“* -[NSKeyedUnarchiver decodeFloatForKey:]: 键 (INFINITY) 的值 (inf) 太大,无法适应 32 位浮点数”
这是 dearchiver 中的错误,还是我有什么问题?