Reputation: 333
I am saving data from a server url to nsuserdefaults as User Detail.
Now at certain point, I want to update 2 keys of User Detail but i am unable to do that. No change occurs.
Here is my code:
NSDictionary *user;
NSMutableDictionary *userMut;
user=[[NSUserDefaults standardUserDefaults] objectForKey:@"User Detail"];
userMut=[user mutableCopy];
[userMut setValue:@"1" forKey:@"isMobileVerified"];
[userMut setValue:mobileNo.text forKey:@"mobile"];
Any help would be appreciated! Plz let me know how to update key values in a dictionary stored in nsuserdefaults if i am doing it wrong.
Upvotes: 2
Views: 4212
Reputation: 2042
You need to synchronize your changes after setting the value:
[[NSUserDefaults standardUserDefaults] setObject:userMut forKey:@"User Detail"];
[[NSUserDefaults standardUserDefaults] synchronize];
Upvotes: 6
Reputation: 122381
You've made a copy of the dictionary and made the changes on that, which won't affect the original copy within the user defaults object.
Setting the dictionary back into the user defaults object should solve the issue:
[[NSUserDefaults standardUserDefaults] setObject:userMut forKey:@"User Detail"];
Upvotes: 2
Reputation: 20410
Try to add the value back when you finish working with it:
[[NSUserDefaults standardUserDefaults] setObject:userMut forKey:@"User Detail"];
You are copying your dictionary, and then doing nothing with that copy
Upvotes: 2