NSNumber与NSString映射的NSDictionary是非属性列表对象

以下代码:

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

这里是来自Xcodedebugging控制台的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 :我发现文字:

value参数只能是属性列表对象:NSData,NSString,NSNumber,NSDate,NSArray或NSDictionary。 对于NSArray和NSDictionary对象,其内容必须是属性列表对象。 请参阅“属性列表编程指南”中的“什么是属性列表?”。

要将字典保存到NSUserDefaults (或将字典作为plist写入文件),唯一的办法是所有的键必须是NSString对象,所有的值必须是有效的plist对象。

换句话说,你不能有NSNumber对象的键。 将它们转换为NSString如果您需要将字典写入NSUserDefaults

有关详细信息,请参阅“ 属性列表编程指南” 。 特别是标题为“ 什么是物业清单”的部分? 。 有一个表格显示了使用的XML标签。 在这张桌子下面,你会看到这个摘录:

虽然NSDictionary和CFDictionary对象允许它们的键是任何types的对象,但如果键不是string对象,则集合不是属性列表对象。