iOS-尝试在NSUserDefaults中插入非属性列表对象NSDictionary [英] iOS - Attempt to insert non-property list object NSDictionary in NSUserDefaults
问题描述
在我的应用中,我需要将自定义的 User
对象 NSDictionary
保存在我的 NSUserDefaults
中.我尝试使用以下代码进行此操作:
In my app, I need to save my custom User
object NSDictionary
in my NSUserDefaults
. I attempt this with the following code:
NSDictionary *userObjectDictionary = response[@"user"];
NSLog(@"USER OBJECT:\n%@", userObjectDictionary);
[defaults setObject:userObjectDictionary forKey:@"defaultUserObjectDictionary"];
[defaults synchronize];
此尝试使我的应用程序崩溃,并显示以下消息:
This attempt crashes my app with the following message:
由于未捕获的异常而终止应用程序"NSInvalidArgumentException",原因:试图插入非属性清单物件{type =不变的字典,计数= 10,条目=> 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:电子邮件= {contents ="myEmail@gmail.com"} 8:{contents ="badge_count"} = {值= +0,类型=kCFNumberSInt64Type} 9:user_name =用户名10:{contents ="phone_number"} = {contents ="0123456789"} 12:{contents ="status_updated_at"} =}为关键defaultUserObjectDictionary'
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
对象字典在尝试保存它之前的样子:
This is what my User
object dictionary looks like before I attempt to save it:
{
"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
对象将具有可为空的属性.
Is it crashing because there a null values in my User
object NSDictionary
? I tried with a regular NSDictionary
with dummy data with null values and it worked. If this is the problem, how to I bypass this? Sometimes my User
object will have properties that are nullable.
推荐答案
字典中的某些对象或键具有类,该类不支持属性列表序列化.请查看此答案以获取详细信息:支持的属性列表ObjC类型
Some of your objects or keys in dictionary have class, that doesn't support property list serialization. Please, see this answer for details: Property list supported ObjC types
我建议检查对象的键"profile_picture"-它可能是NSURL.
I recommend to check object for key "profile_picture" - it may be NSURL.
如果这样做没有帮助,则可以使用以下代码来识别不兼容的对象:
If that doesn't help, you may use following code to identify incompatible object:
for (id key in userObjectDictionary) {
NSLog(@"key: %@, keyClass: %@, value: %@, valueClass: %@",
key, NSStringFromClass([key class]),
userObjectDictionary[key], NSStringFromClass([userObjectDictionary[key] class]));
}
这篇关于iOS-尝试在NSUserDefaults中插入非属性列表对象NSDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!