iDevGames Forums
Cannot load NSData from NSUserDefaults - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Tools & Technology (/forum-10.html)
+--- Thread: Cannot load NSData from NSUserDefaults (/thread-1090.html)

Cannot load NSData from NSUserDefaults - groffhibbitz - Jun 25, 2009 07:51 PM

I'm trying to store some of my games data using NSUserDefaults. I've made a simple class that contains 3 property list objects (two NSNumber and one NSString).

I save and load the data by making my simple object into an NSData object and implementing the following two methods:

-(void)encodeWithCoder:(NSCoder *)encoder
-(id)initWithCoder:(NSCoder *)decoder

when I go to save the data, I encode it first:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:myObject];
[defaults setObject:data forKey:@"VH_myKey"];

then store it off in user defaults (which I've used successfully on other objects).

I load it back with
NSData *data = [defaults dataForKey:@"VH_myKey"];
SimpleClass *myClass = [NSKeyedUnarchiver unarchiveObjectWithData:data];

When I load the data back, my breakpoints in "initWithCoder" get called and it seems like the data is loading back fine...but then when I debug and check the object, there are no values for it's attributes...the debugger says: out of scope or "no value" but every once in a while one of the values will work fine.. anyone know what is going on?

Really I just want to store a custom object with seems like it should be really straightforward... I can post more code if that would help.

Cannot load NSData from NSUserDefaults - SethWillits - Jun 25, 2009 10:25 PM

Don't rely on what the GUI debugger shows you. The display is often wrong. Use po or NSLog it etc. Other than the debugger's display you don't really suggest anything else is wrong so... nothing more to say, really.

Post the code for init and encode though if you're having some other problem (and explain the problem).