0

I have this code

[[[NSUserDefaults standardUserDefaults] valueForKey:@"Identificator"]isEqualToString:@""]
[[[NSUserDefaults standardUserDefaults] valueForKey:@"Identificator"] isKindOfClass:[NSNull class]]) 

How can I verify is it some value for this key, or not. Because this code return NO

rmaddy
  • 314,917
  • 42
  • 532
  • 579

3 Answers3

3

This will help you though.....

 NSUserDefaults *Values = [NSUserDefaults standardUserDefaults];
    if([[[Values dictionaryRepresentation] allKeys] containsObject:@"YourKeyToCheck"])
    {
        //return yes ...... Or Do your stuff here...
    }
Kumar KL
  • 15,315
  • 9
  • 38
  • 60
1

This might help you,

if ([[[[NSUserDefaults standardUserDefaults] valueForKey:@"Identificator"] length] > 0] {
   //empty value
} 
Engnyl
  • 1,380
  • 16
  • 24
0

I don't think that you can have a NULL value for a NSUserDefaults key. Setting the value of a key to nil is equivalent to removing the key. check the accepted answer from this SO question for details

As such, your code correctly returns NO because that key does not exist.

So you should adapt your code accordingly and do whatever you need to do on the NO result, not YES I would say.

Community
  • 1
  • 1
user1459524
  • 3,613
  • 4
  • 19
  • 28