Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tsornin

macrumors newbie
Original poster
Jul 23, 2002
26
0
I'm working through the new Cocoa Programming book and one of the exercises is to add a button for the user to reset their preferences (drop any changes made to their NSUserDefaults and revert to the factory defaults).

In looking at the docco for NSUserDefaults, I see there's a class method "resetStandardUserDefaults" which sounds like it might do what I want... until I read the description: "Synchronizes any changes made to the shared user defaults object and releases it from memory." I'm not sure what the point of that is, but it doesn't seem like it does what I want. I tried it in a bit of code, but nothing actually changed:

Code:
	[NSUserDefaults resetStandardUserDefaults];
	[colorWell setColor:[self tableBgColor]];
	[checkbox setState:[self emptyDoc]];

I'm guessing that just saves the changed prefs out to disk, and then the setColor method reads in the (changed) stuff again. So nothing was actually reset.

Right now I'm thinking the only way to reset the prefs will be to loop through all of the keys in the standard user defaults object and set each one back to whatever the initial value was. Is that the correct way, or am I misunderstanding what resetStandardUserDefaults does?
 
You can create a plist file with "default" values and set that as the standard defaults for use when the pref isn't set yet (new install) or you want to revert to "factory" settings.

Without having the book you are working through I can't say anything one way or another since I don't know what the sample project has in it.

I used this for pre-made default preferences:

Code:
    NSString *userDefaultsValuesPath;
    NSDictionary *userDefaultsValuesDict;
 
    // load the default values for the user defaults
    userDefaultsValuesPath=[[NSBundle mainBundle] pathForResource:@"MacAdShowDefaults" ofType:@"plist"];
    userDefaultsValuesDict=[NSDictionary dictionaryWithContentsOfFile:userDefaultsValuesPath];
 
    // set them in the standard user defaults
    [[NSUserDefaults standardUserDefaults] registerDefaults:userDefaultsValuesDict];
 
Right now I'm thinking the only way to reset the prefs will be to loop through all of the keys in the standard user defaults object and set each one back to whatever the initial value was. Is that the correct way, or am I misunderstanding what resetStandardUserDefaults does?

This seems to be the best way, as [NSUserDefaults resetStandardUserDefaults]; doesn't do what you want to do.

Here is my solution for the same Challenge from "Cocoa Programming in Mac OS X" (Hillegass):

Code:
- (IBAction)resetPreferences:(id)sender
{
   NSColor *defaultColor = [NSColor yellowColor];
   NSData  *colorAsData  = [NSArchiver archivedDataWithRootObject:defaultColor];
   
   
   // reset defaults in the dictionary
   [[NSUserDefaults standardUserDefaults] setObject:colorAsData forKey:DSDTableBackgroundColorKey];
   [[NSUserDefaults standardUserDefaults] setBool:YES           forKey:DSDCreateEmptyDocumentKey];
   
   // update UI: reset default color
   [colorWell setColor:defaultColor];
   
   // update UI: reset default checkbox
   [checkbox setState:YES];
}
 
This seems to be the best way, as [NSUserDefaults resetStandardUserDefaults]; doesn't do what you want to do.

Here is my solution for the same Challenge from "Cocoa Programming in Mac OS X" (Hillegass):

Code:
- (IBAction)resetPreferences:(id)sender
{
   NSColor *defaultColor = [NSColor yellowColor];
   NSData  *colorAsData  = [NSArchiver archivedDataWithRootObject:defaultColor];
   
   
   // reset defaults in the dictionary
   [[NSUserDefaults standardUserDefaults] setObject:colorAsData forKey:DSDTableBackgroundColorKey];
   [[NSUserDefaults standardUserDefaults] setBool:YES           forKey:DSDCreateEmptyDocumentKey];
   
   // update UI: reset default color
   [colorWell setColor:defaultColor];
   
   // update UI: reset default checkbox
   [checkbox setState:YES];
}

Rather than duplicating the logic that defines the values of the defaults, you can simply remove the non-default values stored in [NSUserDefaults standardUserDefaults] using the -(void)removeObjectForKey method.

Here is my solution:
Code:
- (IBAction)resetAllPreferences:(id)sender
{
	NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

	// clear the non-default values
	[userDefaults removeObjectForKey:BNRTableBgColorKey];
	[userDefaults removeObjectForKey:BNREmptyDocKey];

	// update the preference window to reflect the new defaults
	[self windowDidLoad];
}
 
If you want to blow away all your defaults, this snippet has worked for me:

Code:
NSDictionary *defaultsDict = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
for (NSString *key in [defaultsDict allKeys])
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.