how to use NSDictionary in NSUserDefault?

2019-06-09 06:36发布

问题:

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.

回答1:

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"]; 


回答2:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:yourData forKey:@"yourKey"];
[defaults synchronize];

id yourData = [defaults objectForKey:@"yourKey"];