49

I have a NSMutableDictionary, and i have added values to it (several key pair values). Now i require to save this NSMutableDictionary to a NSUserDefaults object.

1.) My code as follows; I am not sure if it is correct , and also i need to know how to retrieve the NSMutableDictionary from the NSUSerDefault ?

2.) After retrieving the NSMutableDictionary i need to save it to a NSDictionary. How could i do these ?

NSMutableDictionary *dic = [[NSMutableDictionary  alloc] init];

[dic addObject:@"sss" forKey:@"hi"];
[dic addObject:@"eee" forKey:@"hr"];

 [NSUserDefaults standardDefaults] setObject:dic forKey:@"DicKey"];
 [[NSUserDefaults standardDefaults] synchronize];
penduDev
  • 4,743
  • 35
  • 37
shajem
  • 2,111
  • 5
  • 22
  • 30
  • 5
    You also need to call `[[NSUserDefaults standardDefaults] synchronize];` to save. What exactly are you struggling with? You've mentioned what you want to do but not where you are having problems. The code you've included above (aside from a missing `[`) looks fine. – Sam Mar 30 '12 at 17:07
  • I need to save some key-value pairs and access it from a different view. So i thought of using NSUserDefaults for this and also NSDictionary. Finally i will have to populate a table with these values. – shajem Mar 30 '12 at 17:30
  • Unless you add more detail, I doubt you will get more answers. You're setting things just fine. Are you not finding the dictionary when you call `[[[NSUserDefaults standardDefaults] objectForKey:@"DicKey"]`? Note that this will be a `NSDictionary*` type and NOT a `NSMutableDictionary*`. If you want to manipulate data across views, consider storing the data in a data model that you pass to both views (or provide a singleton accessor on it depending on how generic / visible you want the data to be). – Sam Mar 30 '12 at 17:44
  • It is not recommended to save to user defaults for access it from another view. You could either send as parameter, or use singleton – Silviu St Jul 08 '15 at 09:28
  • 1
    Note that the use of 'synchronize' isn't needed anymore: https://developer.apple.com/documentation/foundation/nsuserdefaults/1414005-synchronize?language=objc – Hibbem Sep 10 '18 at 12:02

2 Answers2

87

Your code is correct, but the one thing you have to keep in mind is that NSUserDefaults doesn't distinguish between mutable and immutable objects, so when you get it back it'll be immutable:

NSDictionary *retrievedDictionary = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"DicKey"];

If you want a mutable dictionary, just use mutableCopy:

NSMutableDictionary *mutableRetrievedDictionary = [[[NSUserDefaults standardUserDefaults] objectForKey:@"DicKey"] mutableCopy];
yuji
  • 16,695
  • 4
  • 63
  • 64
  • Remember that, using `copy` you are retaining the object, so you'll become the owner and you'll be responsible to release it. – Manlio Mar 30 '12 at 16:48
  • 2
    I am using ARC. And, I need to save a NSMutableDictionary to NSUser Defaults, and get a NSDictionary from a NSUserDefaults. So i have to use your 1st approach i guess ? – shajem Mar 30 '12 at 16:55
  • 3
    Whoops, needed to do `mutableCopy` instead of `copy` to get a mutable dictionary. But yeah, for what you describe, you want the first version. – yuji Mar 30 '12 at 16:59
7
NSMutableDictionary *profileDictionary = [[NSMutableDictionary  alloc] init];
[profileDictionary setObject:txt_Username.text forKey:@"USERNAME_KEY"];
[profileDictionary setObject:txt_Password forKey:@"PASSWORD_KEY"];
[[NSUserDefaults standardUserDefaults] setObject:profileDictionary forKey:@"PROFILE_KEY"];
[[NSUserDefaults standardUserDefaults] synchronize];

This is what you expected.

Shakti
  • 986
  • 12
  • 22
KkMIW
  • 1,092
  • 1
  • 17
  • 27