1

I am experiencing strange behaviour with NSUserDefaults. I am initially storing an array to the user defaults in my AppDelegate.m:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *weekdayIDs = [defaults objectForKey:@"weekdayIDs"];

if (weekdayIDs == nil) {
weekdayIDs = [NSArray arrayWithObjects:@"su", @"mo", @"tu", @"we", @"th", @"fr", @"sa", nil];
[defaults setObject:weekdayIDs forKey:@"weekdayIDs"];
}

[defaults synchronize];

Now in a different view controller ContentViewController.m, I want to retrieve the array:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *weekdayIDs = [defaults objectForKey:@"weekdayIDs"];

But I just get an array without objects, although its count == 7. I also used arrayForKey: but with the same result. I added a screenshot from my breakpoint.

enter image description here

I am regularly using NSUserDefaults, but currently I am bit stuck on this. It's probably a stupid mistake, anyone care to help?

Thank you so much!

-- Update:

I also figured it might be a problem with the init of the NSArray in the first place, but even replacing its objects with manually created NSString *dwid_su = [NSString stringWithString:@"su"]; didn't work.

Julian
  • 408
  • 5
  • 16
  • 2
    Hi Julian. Did you try to `NSLog` your array? Also what do you get if you do a `po weekdayIDs` in your debugger console? – Alladinian Mar 25 '13 at 21:45
  • 1
    I thought so, if you're interested on why you had the problem in the first place, here is the answer http://stackoverflow.com/questions/10963268/float-variables-assigned-value-from-line-above/10963683#10963683 – Alladinian Mar 25 '13 at 21:54

1 Answers1

2

Your code works perfectly.

Just, print the description of you array and you will see what you want.

Right click on weekdayIDs variable and select Print Description of weekdayIDs

or use through lldb debugger console po weekdayIDs

or NSLog(@"%@", weekdayIDs);

Here the results.

enter image description here

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
  • You are right, thanks so much. I thought the array was the problem and was led on the wrong track, as the error was somewhere else. But now I got it. PS: It's already 11 pm in Germany. :-S – Julian Mar 25 '13 at 21:52
  • 1
    @Julian do the sync only when the array is nil. This if you use only that snippet of code you posted otherwise leave as is. But I suppose you write other defaults... – Lorenzo B Mar 25 '13 at 21:57
  • Exactly. There are some defaults I have to initialize. – Julian Mar 25 '13 at 21:58