Cannot load NSData from NSUserDefaults

Posts: 2
Joined: 2009.03
Post: #1
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.
Quote this message in a reply
⌘-R in Chief
Posts: 1,277
Joined: 2002.05
Post: #2
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).
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Getting the freetype library to load on other computers Jake 8 7,850 Jun 30, 2008 06:56 AM
Last Post: Jake
  Xcode 3.0 crashes on project load stevejohnson 7 9,572 Jan 30, 2008 11:12 PM
Last Post: gevapatz
  NSData & zcat unknown 7 5,865 Oct 21, 2006 10:16 AM
Last Post: ThemsAllTook