在我的appdelegate中,我设置了一些自定义NSObject(MemoryObject)的实例并将它们存储到NSMutableDictionary中:
- (void)setMObjects{
_radio = [[Memoryobject alloc]initWithName:@"Radio" andDescription:@"string" andImage:nil];
_fridge = [[Memoryobject alloc]initWithName:@"Fridge" andDescription:@"string" andImage:nil];
[_allMObjects setObject:_radio forKey:@"Radio"];
[_allMObjects setObject:_fridge forKey:@"Fridge"];
}
之后,我将它们存储到NSUserDefaults:
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:_allMObjects] forKey:@"neededMObjects"];
然后,在我的ViewController中,我想要检索这个NSMutableDictionary:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
neededMObjects = [[defaults objectForKey:@"neededMObjects"] mutableCopy];
NSLog(@"dict: %@", neededMObjects);
NSLog(@"object: %@", [neededMObjects objectForKey:@"neededMObjects"]);
第二个NSLog给出了一个错误: - [NSConcreteMutableData objectForKey:]:无法识别的选择器发送到实例0x8c32330
我不明白这一点。为什么错误暗示我使用NSConcreteMutableData,而我使用的是NSMutableDictionary,为什么这不起作用?
答案 0 :(得分:1)
您将将字典存档的结果放入NSUserDefaults。 archivedDataWithRootObject:
的结果是NSData。你需要取消归档才能获得字典。