views:

1100

answers:

3

I'm implementing saved data on my app by using NSUserDefaults, like this:

[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:0],@"mySetting",nil]];
// check
int firstLaunch = [[NSUserDefaults standardUserDefaults] integerForKey:@"mySetting"];
// set
[[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"mySetting"];

Now for development purposes, I'd like to be able to remove the saved data and go back to the defaults, without having to remove the app and re-install it each single time. Is there a quick way to do that? I thought resetStandardUserDefaults would do the job, but it doesn't.

A: 

The docs has this to say about resetStandardUserDefaults:

A subsequent invocation of standardUserDefaults creates a new shared user defaults object with the standard search list.

With some positive (wishful?) thinking it looks like that would be what you need. =)

PEZ
It also says "Synchronizes any changes made to the shared user defaults object", which is a very strong indication that it isn't what Steph needs.
Graham Lee
Not to reanimate this thread after a long sleep (too late) but I'm in the same boat with regard to resetStandardUserDefaults not doing quite the thing I'd expect it to do with a name like _reset_ :\
Joe D'Andrea
LOL (sorry, I know it's not a laughing matter, just so eloquently put by you).
PEZ
+4  A: 

For the key which you wish to revert to the default, -removeObjectForKey: will remove its definition from the app preferences. All things being equal, this means that subsequent invocations of -<type>ForKey: will return the registered default.

Graham Lee
+1  A: 

Have you tried reloading the defaults after calling reset? as PEZ says:

e.g

[NSUserDefaults resetStandardUserDefaults];
[NSUserDefaults standardUserDefaults];

This clears all the defaults and loads in the standard set.

If you only want to blitz one value as it seems from your snippet, then Graham's suggestion of -removeObjectForKey is easier.

EDIT: corrected my misuse of class methods.

Abizern
resetStandardUserDefaults is a class method...
Steph Thirion
Good point, I've edited my reply.
Abizern
that doesn't change the fact that +resetStandardUserDefaults doesn't do what Steph wanted.
Graham Lee