Reputation: 4451
I have a class "MainClass". That class implements NSCoding protocol. In that class I have an array with objects of another class "Object", which also implements NSCoding protocol. When I archive an object of "MainClass" in NSUserDefaults and then unarchive it and try to get the object of class "Object" from array and use it properties then I get message:
[NSConcreteMutableData property]: unrecognized selector sent to instance.
What do I do wrong? How to fix it?
Edit: This is code I use with NSUserDefaults:
- (Settings *) readData
{
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
NSData *dataRepresentingSettings = [currentDefaults objectForKey:@"data"];
if (dataRepresentingSettings != nil) {
return (MainClass *)[NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSettings];
}
return nil;
}
- (void) saveData
{
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:self] forKey:@"data"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
Upvotes: 0
Views: 550
Reputation: 38485
There's two options :
(1) You're not retaining the Object that you got from the user defualts. When you get an object from NSUserDefaults
it's autoreleased - if you want to keep it, you have to retain
it.
(2) You've forgotten to unarchive it. You used NSKeyedArchiver
to archive your Object but didn't unarchive it :)
you might have done something like
Object *object = [defaults objectForKey:@"object"];
instead of
NSData *data = [defaults objectForKey:@"object"];
Object *object = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NB Don't forget that object
in my example is still autoreleased; if you want to keep it, you have to retain it ;)
Upvotes: 1