xcode 为什么 NSUserDefaults 无法保存 NSMutableDictionary?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14105415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:25:42  来源:igfitidea点击:

Why does NSUserDefaults fail to save NSMutableDictionary?

iphoneiosxcodensuserdefaultsnsmutabledictionary

提问by user1940136

I'm trying to save a NSMutableDictionarywith 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?

我正在尝试保存一个NSMutableDictionarywith NSUserDefaults。我在 stackoverflow 中阅读了许多关于该主题的帖子......我还找到了一个有效的选项;然而不幸的是它只工作了一次然后它开始只保存(空)。有人有提示吗?

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:

将对象添加到 的代码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:

NSLog() 值的代码:

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

And also the keys are (null):

而且键是(空):

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

回答by zaph

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

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

The line:

线路:

dictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data];

discards the previously created NSMutableDictionaryand returns a NSDictionary.

丢弃先前创建的NSMutableDictionary并返回一个NSDictionary.

Change the loading to:

将加载更改为:

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

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

完整的例子,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 output:
key: Key 2, value: 1
key: Key 1, value: 0
key: Key 4, value: 4
key: Key 3, value: 2

NSLog 输出:
key: Key 2, value: 1
key: Key 1, value: 0
key: Key 4, value: 4
key: Key 3, value: 2