27

Possible Duplicate:
How can I reset the NSUserDefaults data in the iPhone simulator?

Can we reset NSUserDefaults for all the keys at the same time? Right now I reset for individual keys. So if there is a way to do that in a single go please tell me.

Thanks

Community
  • 1
  • 1
Rachit
  • 1,159
  • 3
  • 13
  • 23

1 Answers1

88

Here is how to do it without looping over all values and removing them.

NSString *domainName = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:domainName];
Mahdi Yusuf
  • 19,931
  • 26
  • 72
  • 101
audience
  • 2,412
  • 21
  • 18
  • 2
    Thanks a lot for your valuable code. – Rachit Jun 15 '11 at 14:26
  • 2
    Yes, there is a method to do this without a loop. See my answer here: http://stackoverflow.com/a/9672855/257550 – memmons Mar 12 '12 at 18:39
  • 2
    It is against apples recommendation to make modifications to a collection object while enumerating it. – Samhan Salahuddin Oct 16 '12 at 05:45
  • 1
    The first Example is not resetting the userdefaults to their default values. – ohcibi Jan 23 '13 at 14:57
  • @ohcibi is right, +resetStandardUserDefaults; doesn't reset the userdefaults to their default value. Here is what it does: "Synchronizes any changes made to the shared user defaults object and releases it from memory." – Bgi Jun 14 '13 at 09:45
  • You're all right. Deleted this part of the answer. – audience Jun 15 '13 at 11:06
  • This doesn't work in unit tests. What worked for me was only to pass `@"xctest"` as the domain name instead. – yonix Dec 23 '18 at 23:12