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!

5
  • 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 Commented Mar 6, 2012 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. Commented Mar 6, 2012 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) Commented Mar 6, 2012 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 Commented Mar 6, 2012 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 developer.apple.com/library/mac/#documentation/Cocoa/Reference/… and implement the required methods, that should fix it Commented Mar 6, 2012 at 22:51

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

1 Comment

Converting your own objects for storing in NSDefaults might be useful too: stackoverflow.com/a/537849/4870
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.