NSDictionary与NSNumber到NSString的映射是非属性列表对象 [英] NSDictionary with NSNumber to NSString mapping is non-property-list object

查看:124
本文介绍了NSDictionary与NSNumber到NSString的映射是非属性列表对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码:

[[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

所以NSPathStore2NSString的子类,为什么此词典不是属性列表?

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.

推荐答案

将字典保存到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.

有关详细信息,请参见

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆