ios - 具有 NSNumber 到 NSString 映射的 NSDictionary 是非属性列表对象

标签 ios objective-c nsdictionary nsuserdefaults property-list

以下代码:

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

所以NSPathStore2NSString的子类,为什么这个字典不是属性列表?

更新:

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

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/

相关文章:

ios - 如何取消在后台运行的线程? (iOS)

ios - 在 UIView addSubview 期间克隆 UIImageView

iphone - 将 ShareKit 添加到我的项目中,无法编译 - 4,448 错误

Swift:作为 CGRect 的字典键

ios - (iOS) 从 Documents 文件夹中读取 plist - 我得到正确的路径但无法加载字典

ios - 观察NSArray中NSDictionary的值变化

iphone - 如何(以编程方式)判断是否有/没有任何支持打开特定文档类型的注册应用程序?

ios - 滚动表格 View 时,标签的角半径变得扭曲

ios - iOS中与真实物体的图像匹配

objective-c - 通过 arraycontroller 获取选中的复选框列表