1

I'm storing all my data in NSUserDefaults. Now I'm trying to store some keys with a specific prefix in an Array. Therefore, I first load the UserDefaults in a Dictionary.

NSString *myPrefix = @"prefix";
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *dict = [defaults dictionaryRepresentation];
for (NSString *keyWithPrefix in dict.keyEnumerator) {
    if ([keyWithPrefix hasPrefix: myPrefix]) {
        [relevantKeys addObject: keyWithPrefix];
    }
}

The Problem is: when I print "dict" (which represents UserDefaults). There are some keys missing. Does NSUserDefaults delete keys, that are not needed temporarily?

smudo78
  • 478
  • 1
  • 3
  • 13

3 Answers3

1

Nope it does not, NSUserDefault is a persistance storage, Please read the following answer it has a very good explanation How persistent is [NSUserDefaults standardUserDefaults]?

Community
  • 1
  • 1
Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
0

No, but are you calling [NSUserdefaults synchronize] when you are updating them ?

Lefteris
  • 14,550
  • 2
  • 56
  • 95
  • ok thanks, my mistake.. The keys, I was searching for, where never written into UserDefaults. :) – smudo78 Jun 05 '12 at 21:24
0

You should probably be using -synchronize method when storing what you want. i,e:

[[NSUserDefaults standardUserDefaults] setValue:someDictionary forKey:@"someKey"];
[[NSUSerDefaults standardUserDefaults] synchronize];
skram
  • 5,314
  • 1
  • 22
  • 26
  • No, unless you're coordinating with a different process that's reading the same preferences (which is unusual), you should not need to invoke `-synchronize`. – Ken Thomases Jun 06 '12 at 08:14