0

I want to know how can I use NSDictionary in NSUserDefault ? I have to store name, points and time of user into the NSDictionary and dictionary into the user default.

Viktor Apoyan
  • 10,655
  • 22
  • 85
  • 147
Maulik
  • 19,348
  • 14
  • 82
  • 137
  • I think you need to clarify how the NSDictionary is being used. Is it being used for the entire NSUserDefaults or for a particular entry in the defaults. – ThomasW May 30 '11 at 08:31
  • ThomasW : i want to store particular entry in the default with multiple values. – Maulik May 30 '11 at 08:39
  • If the dictionary only contains simple objects like strings and numbers you can, but if it contains your own objects you will need to implement the NSCoding protocol on those first. – vakio May 30 '11 at 08:57

2 Answers2

4
NSDictionary *dict = [[NSDictionary alloc] init];

[[NSUserDefaults standardUserDefaults] setObject:dict forKey:@"dict"];

[[NSUserDefaults standardUserDefaults] synchronize];

At the time of retrieval of data,

dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"dict"]; 
visakh7
  • 26,380
  • 8
  • 55
  • 69
Tirth
  • 7,801
  • 9
  • 55
  • 88
2
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:yourData forKey:@"yourKey"];
[defaults synchronize];

id yourData = [defaults objectForKey:@"yourKey"];
cem
  • 3,311
  • 1
  • 18
  • 23