0

I'm trying to store both an integer and NSArray with the NSUserDefaults, but I only can correctly retrieve the integer. When I try to recover the NSArray, it returns an empty array.

I start with a custom class XMLParser.m.

//XMLParser.m

//NSArray 'stored' correctly contains 10 data objects. 'stored' is an NSArray property of the XMLParser class
numberOfEvents = [stored count];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:stored forKey:@"eventsList"];
[defaults setInteger:numberOfEvents forKey:@"numberOfEvents"];
[defaults synchronize];

But when I try to access the data in another class, ie. my AppDelegate, I get an empty NSArray

//myApplicationAppDelegate.m
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
int value = [defaults integerForKey:@"numberOfEvents"];  //returns 10
parsedEventsList = [defaults arrayForKey:@"eventsList"];  //parsedEventsList is an NSArray property of myApplicationAppDelegate class
int value2 = [parsedEventsList count]; //***returns 0***

I've even tried using

[defaults objectForKey:@"eventsList"]

and it's still returning nothing.

Thoughts? Thanks!

rsteier
  • 154
  • 2
  • 12
  • Have you tried NSLog on the array itself, is it nil, or is it empty? Because they would be different things. Maybe try an if statement to see if it is nill – Amit Shah Mar 06 '12 at 22:10
  • what is contained in your array? Are they objects that know how to save themselves into those defaults? My first guess would be your array is saving just fine but the objects in there don't get saved to the file. – AlexTheMighty Mar 06 '12 at 22:17
  • You all are awesome! So quick! @AmitShah - tried an 'if(parsedEventList)...', and it failed. So it is nil. Thoughts on this? user 'sch': (who just appeared to delete their comment) - Tried assigning an NSArray right after '[default synchronize]', for both objectForKey: and arrayForKey:, and both of which return nil (added on what was saying) – rsteier Mar 06 '12 at 22:42
  • @AlexTheMighty - the NSArray is a bunch of custom objects, called Events. Each Event has properties of type NSInteger, NSString, NSDate, NSURL, NSMutableArray, BOOL, and float. I would think that this would be fine. I'll keep you updated – rsteier Mar 06 '12 at 22:42
  • 1
    if its a custom object even if its full of data types chat conform to the NSCoding protocols if the object itself doesn't, they it won't be fine (also you'll need to put that BOOL and float into NSNumber). Take a look at NSCoding Protocol Ref https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Protocols/NSCoding_Protocol/Reference/Reference.html and implement the required methods, that should fix it – AlexTheMighty Mar 06 '12 at 22:51

2 Answers2

1

All of your objects in the stored array must be property list objects (i.e. instances of NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary).

Brian
  • 15,599
  • 4
  • 46
  • 63
  • Converting your own objects for storing in NSDefaults might be useful too: http://stackoverflow.com/a/537849/4870 – waldo Mar 06 '12 at 22:26
0

Your problem appears to be that your objects are all custom. I think you may have forgotten to make them serializable if you add code similar to this (for each of your objects) then it should work

- (void)encodeWithCoder:(NSCoder *)encoder {

   //Encode properties, other class variables, etc
   [encoder encodeObject:self.obj1 forKey:@"obj1"];
   [encoder encodeObject:self.obj2 forKey:@"obj2"];
}

- (id)initWithCoder:(NSCoder *)decoder {
   if((self = [super init])) {
       //decode properties, other class vars
       self.obj1 = [decoder decodeObjectForKey:@"obj1"];
       self.obj2 = [decoder decodeObjectForKey:@"obj2"];
   }
return self;
}
Amit Shah
  • 4,176
  • 2
  • 22
  • 26