iOS - 尝试在 NSUserDefaults 中插入非 属性 列表对象 NSDictionary
iOS - Attempt to insert non-property list object NSDictionary in NSUserDefaults
在我的应用程序中,我需要将我的自定义 User
对象 NSDictionary
保存在我的 NSUserDefaults
中。我尝试使用以下代码:
NSDictionary *userObjectDictionary = response[@"user"];
NSLog(@"USER OBJECT:\n%@", userObjectDictionary);
[defaults setObject:userObjectDictionary forKey:@"defaultUserObjectDictionary"];
[defaults synchronize];
此尝试使我的应用程序崩溃并显示以下消息:
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: 'Attempt to insert non-property
list object {type =
immutable dict, count = 10, entries => 0 : status_id = 1 : {contents = "first_name"} = exampleFirstName 3 : id = {value = +2, type =
kCFNumberSInt64Type} 4 : {contents = "profile_picture"} = {contents =
"http://myDevServer.com/pictures/userName.jpg"} 5 :
last_name = exampleLastName 7 : email = {contents = "myEmail@gmail.com"} 8 : {contents = "badge_count"} = {value = +0, type =
kCFNumberSInt64Type} 9 : user_name = userName 10 : {contents = "phone_number"} = {contents = "0123456789"} 12 : {contents = "status_updated_at"} =
} for key
defaultUserObjectDictionary'
这是我的 User
对象字典在尝试保存之前的样子:
{
"badge_count" = 0;
email = "myEmail@gmail.com";
"first_name" = exampleFirstName;
id = 2;
"last_name" = exampleLastName;
"phone_number" = 0123456789;
"profile_picture" = "http://myDevServer.com/pictures/userName.jpg";
"status_id" = "<null>";
"status_updated_at" = "<null>";
"user_name" = userName;
}
是否因为我的 User
对象 NSDictionary
中有空值而崩溃?我尝试使用带有空值的虚拟数据的常规 NSDictionary
并且它有效。如果这是问题所在,我该如何绕过它?有时我的 User
对象将具有可为空的属性。
您的某些对象或字典中的键具有 class,不支持 属性 列表序列化。请参阅此答案了解详情:Property list supported ObjC types
我建议检查对象中的键 "profile_picture" - 它可能是 NSURL。
如果这没有帮助,您可以使用以下代码来识别不兼容的对象:
for (id key in userObjectDictionary) {
NSLog(@"key: %@, keyClass: %@, value: %@, valueClass: %@",
key, NSStringFromClass([key class]),
userObjectDictionary[key], NSStringFromClass([userObjectDictionary[key] class]));
}
NSNull 在 JSON 中是可接受的(如 "<null>"
),但未在 属性 列表中列出。因此,一种解决方案是将字典转换为 NSData 并保存。
在我的应用程序中,我需要将我的自定义 User
对象 NSDictionary
保存在我的 NSUserDefaults
中。我尝试使用以下代码:
NSDictionary *userObjectDictionary = response[@"user"];
NSLog(@"USER OBJECT:\n%@", userObjectDictionary);
[defaults setObject:userObjectDictionary forKey:@"defaultUserObjectDictionary"];
[defaults synchronize];
此尝试使我的应用程序崩溃并显示以下消息:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object {type = immutable dict, count = 10, entries => 0 : status_id = 1 : {contents = "first_name"} = exampleFirstName 3 : id = {value = +2, type = kCFNumberSInt64Type} 4 : {contents = "profile_picture"} = {contents = "http://myDevServer.com/pictures/userName.jpg"} 5 : last_name = exampleLastName 7 : email = {contents = "myEmail@gmail.com"} 8 : {contents = "badge_count"} = {value = +0, type = kCFNumberSInt64Type} 9 : user_name = userName 10 : {contents = "phone_number"} = {contents = "0123456789"} 12 : {contents = "status_updated_at"} = } for key defaultUserObjectDictionary'
这是我的 User
对象字典在尝试保存之前的样子:
{
"badge_count" = 0;
email = "myEmail@gmail.com";
"first_name" = exampleFirstName;
id = 2;
"last_name" = exampleLastName;
"phone_number" = 0123456789;
"profile_picture" = "http://myDevServer.com/pictures/userName.jpg";
"status_id" = "<null>";
"status_updated_at" = "<null>";
"user_name" = userName;
}
是否因为我的 User
对象 NSDictionary
中有空值而崩溃?我尝试使用带有空值的虚拟数据的常规 NSDictionary
并且它有效。如果这是问题所在,我该如何绕过它?有时我的 User
对象将具有可为空的属性。
您的某些对象或字典中的键具有 class,不支持 属性 列表序列化。请参阅此答案了解详情:Property list supported ObjC types
我建议检查对象中的键 "profile_picture" - 它可能是 NSURL。
如果这没有帮助,您可以使用以下代码来识别不兼容的对象:
for (id key in userObjectDictionary) {
NSLog(@"key: %@, keyClass: %@, value: %@, valueClass: %@",
key, NSStringFromClass([key class]),
userObjectDictionary[key], NSStringFromClass([userObjectDictionary[key] class]));
}
NSNull 在 JSON 中是可接受的(如 "<null>"
),但未在 属性 列表中列出。因此,一种解决方案是将字典转换为 NSData 并保存。