Reputation: 21237
I have a string that downloads from a server. It is in JSON format and is well-formed. It is an array of JSON objects. My objective is to convert this to an NSArray
and then store it in NSUserDefaults
for subsequent use. My attempt generally works, but sometimes I crash with this error:
Attempt to set a non-property-list object
Here is the code that I am using. I think that it should be this straightforward. I don't believe have to actually convert the objects to NSDictionaries
and iterate over this, do I?
NSString *message = [dict objectForKey:@"message"];
NSLog(@"message: %@", message);
data = [message dataUsingEncoding:NSUTF8StringEncoding];
NSArray *arr = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
[[NSUserDefaults standardUserDefaults] setObject:arr forKey:RESOURCES_LIST];
For example, this is one of the strings that causes this to crash (as output by the NSLog
command):
[{"folder":"Documents","files":[{"sort_order":"120","filename":"pdf.pdf","filetype":"pdf","display_name":"Instructions","upload_date":"2015-08-11","md5":"ea9f839f91941b5ea7f5a316e3ce95ca","bool_external":"0","url":"http://www.somesite.com/pdf.pdf"}]},{"folder":"Images","files":[{"sort_order":"100","filename":"space.jpg","filetype":"image","display_name":"example","upload_date":"2015-10-14","md5":"bc63b896949cbf87c54678fee8ed833b","bool_external":"0","url":"http://www.somesite.com/space.jpg"},{"sort_order":"110","filename":"profile.png","filetype":"image","display_name":"Profile","upload_date":"2015-10-14","md5":"740d61911560e1c84869563b83f3bbf8","bool_external":"0","url":"http://www.somesite.com/profile.jpeg"}]},{"folder":"Info","files":[{"sort_order":"130","filename":"info.pdf","filetype":"pdf","display_name":"info","upload_date":"2015-11-17","md5":"926a7941cc9c7f58e43c3eb2de661c27","bool_external":"0","url":null}]},{"folder":"Videos","files":[{"sort_order":"130","filename":"sample_video","filetype":"video","display_name":"Instructional Video","upload_date":"2015-08-11","md5":"-1","bool_external":"0","url":"https://www.youtube.com/embed/PuNIwSsz7PI"}]}]
Upvotes: 0
Views: 89
Reputation: 56089
"url": null
(in [2][@"files"][0]
)
This is likely being parsed into a value of [NSNull null]
, which cannot be stored in NSUserDefaults
. You will need to either change the server to send as an empty string or not at all, or (recursively) iterate and check all values, removing (or replacing with @""
) all keys with value [NSNull null]
.
For reference, the acceptable classes in plist files and NSUserDefaults
are:
And keys must be strings.
Upvotes: 1