Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDerek Medlin
6,151 PointsNSDictionary to NSUserDefaults
Is there a way to store NSDictionary keys and objects to NSUserDefaults? I mean i know you can...
NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
[defs setObject:[myDictionary objectForKey:@"myKey"] forKey:@"myKey"]; // continue for every object
But is there some sort of initialization I can do? Basically something that creates the NSUserDefaults from the dictionary. If not I guess ill just have to keep manually adding all the objects every time. Thanks.
1 Answer
Pavel Palancica
6,535 PointsHey Derek,
In iOS everything is possible :). Here is the solution for you:
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"Pavel", @"FirstName", @"Palancica", @"LastName", nil];
NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
[defs registerDefaults:dict]; [defs synchronize];
NSLog(@"First Name = %@", [defs objectForKey:@"FirstName"]); NSLog(@"Last Name = %@", [defs objectForKey:@"LastName"]);
Hope it saved your day.
Regards!!!
Derek Medlin
6,151 PointsDerek Medlin
6,151 PointsThanks! I literally just found this out a few seconds ago! Good stuff!