SDictionary与NSNumber到NSString的映射

SDictionary与NSNumber到NSString的映射

本文介绍了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?

更新:

此处 https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/nsuserdefaults_Class/Reference/Reference.html#//apple_ref/occ/instm /NSUserDefaults/setObject:forKey :我找到了文本:

推荐答案

将字典保存到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的映射是非属性列表对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:51