以下代码:
[[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
所以
NSPathStore2
是 NSString
的子类,为什么这个字典不是属性列表?更新:
这里 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 对象。
换句话说,您不能拥有 NSNumber
对象的键。如果您需要将字典写入 NSString
,请将它们转换为 NSUserDefaults
。
有关这方面的详细信息,请参阅 Property List Programming Guide 。特别是标题为 What is a Property List? 的部分。有一个表格显示了所使用的 XML 标签。在该表下,您将看到以下摘录:
关于ios - 具有 NSNumber 到 NSString 映射的 NSDictionary 是非属性列表对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22704678/