Raf*_*afi 2 objective-c nsdictionary nsuserdefaults ios
在我的应用程序中,我需要将自定义User对象保存NSDictionary在中NSUserDefaults。我尝试使用以下代码进行此操作:
NSDictionary *userObjectDictionary = response[@"user"];
NSLog(@"USER OBJECT:\n%@", userObjectDictionary);
[defaults setObject:userObjectDictionary forKey:@"defaultUserObjectDictionary"];
[defaults synchronize];
Run Code Online (Sandbox Code Playgroud)
这种尝试使我的应用程序崩溃,并显示以下消息:
由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“试图插入非属性列表对象{type =不可变的dict,count = 10,条目=> 0:status_id = 1:{contents =“ first_name”} = exampleFirstName 3 :id = {值= +2,类型= 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'
这是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;
}
Run Code Online (Sandbox Code Playgroud)
是否由于我的User对象中存在null值而导致崩溃NSDictionary?我尝试使用NSDictionary带有空值的伪数据的常规数据,它可以正常工作。如果这是问题,我该如何绕过?有时我的User对象将具有可为空的属性。
词典中的某些对象或键具有类,该类不支持属性列表序列化。请查看此答案以获取详细信息:属性列表支持的ObjC类型
我建议检查对象的键“ profile_picture”-它可能是NSURL。
如果这样做没有帮助,则可以使用以下代码来识别不兼容的对象:
for (id key in userObjectDictionary) {
NSLog(@"key: %@, keyClass: %@, value: %@, valueClass: %@",
key, NSStringFromClass([key class]),
userObjectDictionary[key], NSStringFromClass([userObjectDictionary[key] class]));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5131 次 |
| 最近记录: |