为什么NSUserDefaults无法保存NSMutableDictionary?(Why does NSUserDefaults fail to save NSMutableDictionary?)

我试图用NSUserDefaults保存一个NSMutableDictionary 。 我在stackoverflow上阅读了很多关于这个主题的文章...我也发现了一个可以工作的选项; 然而不幸的是,它只工作了一次,然后它开始只保存(空)。 有人有提示吗?

谢谢

要保存的代码:

[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:dictionary] forKey:@"Key"]; [[NSUserDefaults standardUserDefaults] synchronize];

加载代码:

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init]; NSData *data = [[NSUserDefaults standardUserDefaults]objectForKey:@"Key"]; dictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data];

将对象添加到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"];

代码到NSLog()值:

for (NSString * key in [dictionary allKeys]) { NSLog(@"key: %@, value: %i", key, [[dictionary objectForKey:key]integerValue]); }

还有这些键是(null):

NSLog(@"%@"[dictionary allKeys]);

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]);

最满意答案

从Apple的NSUserDefaults objectForKey的文档: 返回的对象是不可变的 ,即使您最初设置的值是可变的。

该行:

dictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data];

丢弃之前创建的NSMutableDictionary并返回一个NSDictionary 。

将加载更改为:

NSData *data = [[NSUserDefaults standardUserDefaults]objectForKey:@"Key"]; dictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data];

完整的例子,在这个例子中也不需要使用NSKeyedArchiver :

NSDictionary *firstDictionary = @{@"Key 4":@4}; [[NSUserDefaults standardUserDefaults] setObject:firstDictionary forKey:@"Key"]; NSMutableDictionary *dictionary = [[[NSUserDefaults standardUserDefaults] objectForKey:@"Key"] mutableCopy]; dictionary[@"Key 1"] = @0; dictionary[@"Key 2"] = @1; dictionary[@"Key 3"] = @2; for (NSString * key in [dictionary allKeys]) { NSLog(@"key: %@, value: %@", key, [dictionary objectForKey:key]); }

NSLog输出: 键:键2,值:1 键:键1,值:0 键:键4,值:4 键:键3,值:2

From Apple's documentation for NSUserDefaults objectForKey: The returned object is immutable, even if the value you originally set was mutable.

The line:

dictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data];

discards the previously created NSMutableDictionary and returns a NSDictionary.

Change the loading to:

NSData *data = [[NSUserDefaults standardUserDefaults]objectForKey:@"Key"]; dictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data];

Complete example, there is also no need to use NSKeyedArchiver in this example:

NSDictionary *firstDictionary = @{@"Key 4":@4}; [[NSUserDefaults standardUserDefaults] setObject:firstDictionary forKey:@"Key"]; NSMutableDictionary *dictionary = [[[NSUserDefaults standardUserDefaults] objectForKey:@"Key"] mutableCopy]; dictionary[@"Key 1"] = @0; dictionary[@"Key 2"] = @1; dictionary[@"Key 3"] = @2; for (NSString * key in [dictionary allKeys]) { NSLog(@"key: %@, value: %@", key, [dictionary objectForKey:key]); }

NSLog output: key: Key 2, value: 1 key: Key 1, value: 0 key: Key 4, value: 4 key: Key 3, value: 2

为什么NSUserDefaults无法保存NSMutableDictionary?(Why does NSUserDefaults fail to save NSMutableDictionary?)

我试图用NSUserDefaults保存一个NSMutableDictionary 。 我在stackoverflow上阅读了很多关于这个主题的文章...我也发现了一个可以工作的选项; 然而不幸的是,它只工作了一次,然后它开始只保存(空)。 有人有提示吗?

谢谢

要保存的代码:

[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:dictionary] forKey:@"Key"]; [[NSUserDefaults standardUserDefaults] synchronize];

加载代码:

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init]; NSData *data = [[NSUserDefaults standardUserDefaults]objectForKey:@"Key"]; dictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data];

将对象添加到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"];

代码到NSLog()值:

for (NSString * key in [dictionary allKeys]) { NSLog(@"key: %@, value: %i", key, [[dictionary objectForKey:key]integerValue]); }

还有这些键是(null):

NSLog(@"%@"[dictionary allKeys]);

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]);

最满意答案

从Apple的NSUserDefaults objectForKey的文档: 返回的对象是不可变的 ,即使您最初设置的值是可变的。

该行:

dictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data];

丢弃之前创建的NSMutableDictionary并返回一个NSDictionary 。

将加载更改为:

NSData *data = [[NSUserDefaults standardUserDefaults]objectForKey:@"Key"]; dictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data];

完整的例子,在这个例子中也不需要使用NSKeyedArchiver :

NSDictionary *firstDictionary = @{@"Key 4":@4}; [[NSUserDefaults standardUserDefaults] setObject:firstDictionary forKey:@"Key"]; NSMutableDictionary *dictionary = [[[NSUserDefaults standardUserDefaults] objectForKey:@"Key"] mutableCopy]; dictionary[@"Key 1"] = @0; dictionary[@"Key 2"] = @1; dictionary[@"Key 3"] = @2; for (NSString * key in [dictionary allKeys]) { NSLog(@"key: %@, value: %@", key, [dictionary objectForKey:key]); }

NSLog输出: 键:键2,值:1 键:键1,值:0 键:键4,值:4 键:键3,值:2

From Apple's documentation for NSUserDefaults objectForKey: The returned object is immutable, even if the value you originally set was mutable.

The line:

dictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data];

discards the previously created NSMutableDictionary and returns a NSDictionary.

Change the loading to:

NSData *data = [[NSUserDefaults standardUserDefaults]objectForKey:@"Key"]; dictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data];

Complete example, there is also no need to use NSKeyedArchiver in this example:

NSDictionary *firstDictionary = @{@"Key 4":@4}; [[NSUserDefaults standardUserDefaults] setObject:firstDictionary forKey:@"Key"]; NSMutableDictionary *dictionary = [[[NSUserDefaults standardUserDefaults] objectForKey:@"Key"] mutableCopy]; dictionary[@"Key 1"] = @0; dictionary[@"Key 2"] = @1; dictionary[@"Key 3"] = @2; for (NSString * key in [dictionary allKeys]) { NSLog(@"key: %@, value: %@", key, [dictionary objectForKey:key]); }

NSLog output: key: Key 2, value: 1 key: Key 1, value: 0 key: Key 4, value: 4 key: Key 3, value: 2