Reputation: 51
I am new to iOS development. I have a NSMutableArray
that is populated with a dictionary. I need to save the NSMutableArray
in NSUserDefaults
.
This is the data that I have passed to my NSMutableArray
:
{
"sub_slots" ={
address = "501 Balestier Road #02-01 Wai Wing Centre";
"avail_id" = 2230;
"branch_id" = 600;
"branch_name" = Balestier;
"company_description" = "At Action X, we design, produce and deliver highly interactive sports events around the world. Through innovation, we revolutionize the sports entertainment landscape with the simple goal of creating happiness for people.";
"company_id" = 222;
"company_name" = "Action X";
"d_salary_from" = 7;
"d_salary_to" = 7;
date = "<null>";
description = "Interview: Face to face interview period from 4th - 8th May (in office)
Working hours: 7am - 10am and 11am - 2pm (1 hour lunch break from 10am to 11am)
Minimum 5 working days (excluding PH)
Generating sponsorship leads in Australia.
Payment method: Cheque";
direction = "From Toa Payoh MRT Station (NS19).
Walk 109 m to Toa Payoh Interchange.
Board Bus 139, 145 at Toa Payoh Interchange, alight at Opposite Moulmein Community Center, Balestier Road, 3 stops later.
Walk 34 m to Wai Wing Centre.";
"end_date_time" = "";
"expired_at" = "2015-05-08T19:21:01.000+08:00";
followed = 0;
grade = 2;
"incentives_and_benefits" = "{\"food\":{\"0\":\"\"},\"commission\":{\"1\":\"$2 for first 9 appointments, $5 for all appointments if minimum 10 appointments hit.\"},\"uniform\":{\"0\":\"\"},\"transport\":{\"0\":\"\"},\"extras\":{\"0\":\"\"}}";
"is_anytime" = 1;
"is_permanent" = 1;
"is_quick_job" = 0;
"job_function_name" = Telemarketer;
latitude = "1.326446";
location = "1.326446,103.84705";
longitude = "103.84705";
"main_slot_id" = 2230;
"mandatory_requirements" = (
"Good communication skills in English",
"Well groomed, pleasant and positive work attitude",
"Need to commit for 1 month"
);
"offered_salary" = 7;
"optional_requirements" = (
);
"postal_code" = 329844;
priority = 1;
"publish_salary" = 1;
published = 1;
"start_date_time" = "";
status = MA;
"sub_slot_id" = 2230;
"ts_end_date_time" = 0;
"ts_expired_at" = 1431084061;
"ts_start_date_time" = 0;
zone = central;
};
},
}
I've tried saving it using:
[[NSUserDefaults standardUserDefaults] setObject:myMutableArray forKey:@"locations"];
[[NSUserDefaults standardUserDefaults] synchronize];
When i try to run the application, it crashes in this block of code. This is the error that i am getting:
2015-05-08 18:19:29.559 Matchimi[477:82076] Property list invalid for format: 200 (property lists cannot contain objects of type 'CFNull')
2015-05-08 18:19:29.654 Matchimi[477:82076] Attempt to set a non-property-list object ( ) for key locations.
Any help will be appreciated. Thank you.
Upvotes: 0
Views: 152
Reputation: 122458
What's happening is that "<null>"
indicates that one of the values you are trying to store is of type NSNull
. This type cannot be stored within NSUserDefaults
, so you will need to remove any key/value pairs where the value is NSNull
before storing it:
NSMutableArray *keysToRemove = [NSMutableArray new];
NSArray *keys = [dict allKeys];
for (NSString *key in keys) {
id value = dict[key];
if ([value isKindOfClass:[NSNull class]])
[keysToRemove addObject:key];
}
for (NSString *key in keysToRemove]
[dict removeObjectForKey:key];
(this assumes that dict
is a NSMutableDictionary
).
When you read the data back in from NSUserDefaults
be prepared for missing key/value pairs and if you find any you can assume their value was null.
Upvotes: 1
Reputation: 1622
You can store dictionary,array or any custom object in NSUserDefaults using:
[[NSUserDefaults standardUserDefaults]setObject:[NSKeyedArchiver archivedDataWithRootObject:array] forKey:@"YOUR KEY"];
[[NSUserDefaults standardUserDefaults]synchronize];
& you can retrive it using:
[NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults]objectForKey:@"YOUR KEY"]]
Upvotes: 0
Reputation: 4005
Check your date = "<null>";
is null and with that value you can not add it into UserDefault.
Upvotes: 0