NSDictionary与NSNumber到NSString的映射是非属性列表对象 [英] NSDictionary with NSNumber to NSString mapping is non-property-list object
问题描述
以下代码:
[[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?
更新:
此处
value参数只能是属性列表对象:NSData,NSString,NSNumber,NSDate,NSArray或NSDictionary.对于NSArray和NSDictionary对象,它们的内容必须是属性列表对象.请参阅什么是财产清单?"在属性列表编程指南"中.
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.
将字典保存到 The only way you can save a dictionary to 换句话说,您不能拥有作为 In other words, you can't have keys that are 有关详细信息,请参见什么是属性列表?.有一个表显示使用的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和CFDictionary对象允许它们的键成为任何类型的对象,但是如果键不是字符串对象,则集合也不是属性列表对象. 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. 这篇关于NSDictionary与NSNumber到NSString的映射是非属性列表对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
推荐答案
NSUserDefaults
(或将字典作为plist写入文件的唯一方法),所有键都必须是NSString
对象并且所有值都必须是有效的plist对象.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
.NSNumber
objects. Convert them to NSString
if you need to write the dictionary to NSUserDefaults
.