1
votes

I have an app which stores data using NSKeyedArchiver and everything was working fine.

In the first version of the app a value X was stored as a string, but now I changed it to be stored and saved as an int. The problem is if someone updates from the old version to the newest the app crashes because intForKey: gets called on a key containing a String.

Is there some way during decoding to check if what is being decoded is an int or an object?

1
Are you actually calling intForKey: or decodeIntegerForKey: method? Can you post a code snippet showing how you're doing this? - JRG-Developer
Sorry, I meant int x = [aDecoder decodeIntForKey:@"value"]; - halfblood17

1 Answers

1
votes

NSKeyedArchiver doesn't actually encode primitive values. Instead, it wraps them in an NSNumber.

Perhaps, have you tried using decodeObjectForKey: instead of decodeIntegerForKey:?

In example,

id xObject = [decoder decodeObjectForKey:@"xObjectKey"];
self.x = [xObject integerValue];

// I believe this should work because xObject will either by an NSNumber or NSString
// Both of which respond to the selector integerValue