这个问题在这里已经有了答案:
Best Answer-推荐答案 strong>
您的代码似乎不错。我找不到错误所以尝试换行:
self.myDictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data];
到
id unknownObject = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(@"%@",[unknownObject class]);
看看@控制台。如果输出是字典,也许你也应该尝试强制转换。所以尝试将其更改为:
self.myDictionary = (NSDictionary*)[NSKeyedUnarchiver unarchiveObjectWithData:data];
编辑
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys"object1",@"key1",@"object2",@"key2",@"object3",@"key3", nil];
NSLog(@"before: %@",dictionary);
NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:dictionary];
NSDictionary *myDictionary = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:myData];
NSLog(@"after: %@",myDictionary);
输出:
2013-11-13 14:32:31.369 DemoM[175:60b] before: {
key1 = object1;
key2 = object2;
key3 = object3;
}
2013-11-13 14:32:31.372 DemoM[175:60b] after: {
key1 = object1;
key2 = object2;
key3 = object3;
}
关于ios - 将 NSDictionary(带有自定义类)保存到 NSUserDefaults,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/19954845/