【发布时间】:2012-12-31 19:31:04
【问题描述】:
我正在尝试用NSUserDefaults 保存NSMutableDictionary。我在 stackoverflow 中阅读了许多关于该主题的帖子......我还找到了一个可行的选项;然而不幸的是它只工作了一次,然后它开始只保存(null)。
有人有提示吗?
谢谢
要保存的代码:
[[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]);
【问题讨论】:
-
非常好的问题!
标签: iphone ios xcode nsuserdefaults nsmutabledictionary