以下代码:
[[NSUserDefaults standardUserDefaults] setObject:photoIdToPath forKey:@"photo_id_to_path"];
给我以下错误:
Attempt to set a non-property-list object {
417675729 = "/Users/k06a/Library/Application Support/iPhone Simulator/7.1/Applications/21163736-DF6F-4E79-8196-6A966C81ED1B/Documents/417675729";
} as an NSUserDefaults value for key photo_id_to_path
这里是 Xcode 调试控制台的 photoIdToPath
字典内容分析:
(lldb) po photoIdToPath
{
417675729 = "/Users/k06a/Library/Application Support/iPhone Simulator/7.1/Applications/21163736-DF6F-4E79-8196-6A966C81ED1B/Documents/417675729";
}
(lldb) po [[[photoIdToPath allKeys] lastObject] class]
__NSCFNumber
(lldb) po [[[photoIdToPath allValues] lastObject] class]
NSPathStore2
所以NSPathStore2
是NSString
的子类,为什么这个字典不是属性列表?
更新:
The value parameter can be only property list objects: NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. For NSArray and NSDictionary objects, their contents must be property list objects. See “What is a Property List?” in Property List Programming Guide.
最佳答案
将字典保存到 NSUserDefaults
的唯一方法(或将字典作为 plist 写入文件),所有键必须是 NSString
对象,所有值必须是有效的 plist 对象。
换句话说,您不能拥有作为NSNumber
对象的键。如果您需要将字典写入 NSUserDefaults
,请将它们转换为 NSString
。
有关这方面的详细信息,请参阅 Property List Programming Guide .特别是标题为 What is a Property List? 的部分.有一个表格显示了所使用的 XML 标签。在该表下,您将看到以下摘录:
And although NSDictionary and CFDictionary objects allow their keys to be objects of any type, if the keys are not string objects, the collections are not property-list objects.
关于ios - 具有 NSNumber 到 NSString 映射的 NSDictionary 是非属性列表对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22704678/