I am trying to save a NSDictionary
with array values to NSUserDefaults
but am having some strange trouble.
My NSDictionary
has NSStrings
for keys
and each value
is a NSArray
of NSNumbers
. When I print the dictionary out, everything is fine. I write this dictionary to NSUserDefaults
and if I read it back out right away, everything seams fine. Using this everything seams just fine:
[[NSUserDefaults standardUserDefaults] setObject:self.selectedOptionPositions
forKey:PREF_OPTIONS_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
//THIS PRINT EVERYTHING OUT EXACTLY AS IT SHOULD!
NSLog(@"read after write: %@", [[NSUserDefaults standardUserDefaults]
objectForKey:PREF_OPTIONS_KEY]);
The problem comes when I create a new instance of the class that handles this. When I make a new instance of the class and in the init method check the NSDictionary
like so:
NSLog(@"read initial: %@", [[NSUserDefaults standardUserDefaults]
objectForKey:PREF_OPTIONS_KEY]);
When I print that logging, the NSDictionary
contains all of the keys but all of the values
are now empty! All newly added keys
exist after recreating the class, but no values
persist.
What could be wrong here? There are no warnings or errors in the console.