Question

I have to archive a float with value INFINITY, and later to dearchive it.
Here is my example code:

Object to be archived:

@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

And here is the archiving/dearchiving code:

CodeInf *myCodeInf = [[CodeInf alloc] init];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:myCodeInf];
myCodeInf = [NSKeyedUnarchiver unarchiveObjectWithData:data];

Archiving works, but dearchiving raises the error:

* 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?

Was it helpful?

Solution

Looks like a bug. Submit a bug report to Apple.

As a workaround use encodeDouble and decodeDoubleForKey - you can keep your value as a float and no casts are needed by C rules.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top