问题描述
以下代码:
[[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
词典内容分析:
Here is photoIdToPath
dictionary content analysis from Xcode debug console:
(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
的子类,为什么此词典不是属性列表?
So NSPathStore2
is subclass of NSString
, why this dictionary is not property list?
更新:
推荐答案
将字典保存到NSUserDefaults
(或将字典作为plist写入文件的唯一方法),所有键都必须是NSString
对象并且所有值都必须是有效的plist对象.
The only way you can save a dictionary to NSUserDefaults
(or write the dictionary to a file as a plist), all keys MUST be NSString
objects and all values must be valid plist objects.
换句话说,您不能拥有作为NSNumber
对象的键.如果需要将字典写到NSUserDefaults
,请将它们转换为NSString
.
In other words, you can't have keys that are NSNumber
objects. Convert them to NSString
if you need to write the dictionary to NSUserDefaults
.
有关详细信息,请参见属性列表编程指南.特别是标题为什么是属性列表?.有一个表显示使用的XML标签.在该表下,您将看到以下摘录:
For details on this, see the Property List Programming Guide. Specifically the section titled What is a Property List?. There is a table showing the XML tags used. Under that table you will see this excerpt:
这篇关于NSDictionary与NSNumber到NSString的映射是非属性列表对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!