Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removePersistentDomainForName not working for NSUserDefaults

I'm writing an OS X app. I'm trying to clear NSUserDefaults.

I'm currently doing:

[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]];

But it's not working, i.e. all the key-value pairs are still there when I print the user defaults.

I've also tried adding:

[[NSUserDefaults standardUserDefaults] synchronize];

still no luck.

Can anyone please help? Thanks in advance!

like image 279
Emma Li Avatar asked Dec 04 '25 13:12

Emma Li


1 Answers

It seems that removePersistentDomainForName: does not work on OS X 10.8. Furthermore, these things are not functional also:

[[NSUserDefaults standardUserDefaults] setPersistentDomain:@{} forName:[NSBundle mainBundle].bundleIdentifier];

[[NSUserDefaults standardUserDefaults] setValuesForKeysWithDictionary:@{}];

To work around the problem, you may set an arbitrary key-value pair to the domain then remove the pair:

[[NSUserDefaults standardUserDefaults] setPersistentDomain:@{@"": @""} forName:[NSBundle mainBundle].bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@""];

NSLog(@"%@", [[NSUserDefaults standardUserDefaults] persistentDomainForName:[NSBundle mainBundle].bundleIdentifier]); // => (null)
like image 67
uasi Avatar answered Dec 09 '25 15:12

uasi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!