gpt4 book ai didi

iOS - 尝试在 NSUserDefaults 中插入非属性列表对象 NSDictionary

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:00:52 26 4
gpt4 key购买 nike

在我的应用程序中,我需要在我的 NSUserDefaults 中保存我的自定义 User 对象 NSDictionary。我尝试使用以下代码:

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 对象将具有可为 null 的属性。

最佳答案

您的某些对象或字典中的键具有不支持属性列表序列化的类。请查看此答案以获取详细信息: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]));
}

关于iOS - 尝试在 NSUserDefaults 中插入非属性列表对象 NSDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35496044/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com