I’m trying to save a NSMutableDictionary
with NSUserDefaults
. I read many posts on the topic in stackoverflow... I also found one option that worked; however unfortunately it worked only once and then it started to save (null) only.
Does anybody have a hint?
Thanks
Code to save:
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:dictionary] forKey:@"Key"];
[[NSUserDefaults standardUserDefaults] synchronize];
Code to load:
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init];
NSData *data = [[NSUserDefaults standardUserDefaults]objectForKey:@"Key"];
dictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data];
Code to add Objects to the NSMutableDictionary
:
[dictionary setObject:[NSNumber numberWithInt:0] forKey:@"Key 1"];
[dictionary setObject:[NSNumber numberWithInt:1] forKey:@"Key 2"];
[dictionary setObject:[NSNumber numberWithInt:2] forKey:@"Key 3"];
Code to NSLog() values:
for (NSString * key in [dictionary allKeys]) {
NSLog(@"key: %@, value: %i", key, [[dictionary objectForKey:key]integerValue]);
}
And also the keys are (null):
NSLog(@"%@"[dictionary allKeys]);