-2

I have the following code

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSValue *state = [defaults objectForKey:@"screenstatus"];

Here the state holds a string value for the key screen status. I need to get the value of the screen status and compare it with a string say the string to compare with is "abcd"

if (CODE TO COMPARE){
    //Has ran before, skip your UIViews or whatever
}
else{
    //Has not ran before, do your setup or whatever.
}

I am not clear what I need to write in the CODE TO COMPARE section. Could you please help me .

Thanks for your time.

Timothy Rajan
  • 1,947
  • 8
  • 38
  • 62
  • 2
    If it's a string, then `state` isn't an `NSValue` but an `NSString`. –  Nov 23 '13 at 08:00
  • state is just logical name. i will change it to status. Can you help me pls.. – Timothy Rajan Nov 23 '13 at 08:03
  • 2
    If the Value itself a string , then why do you need to assign as NSValue.. **if([defaults objectForKey:@"screenstatus"] isEqualToString : @"abcd"){}** – Kumar KL Nov 23 '13 at 08:05
  • 1
    @Timothy What's all that whining about the "logical" name? I don't have any problem with how you name your variable. I'm just saying that it isn't going to be an `NSValue` but an `NSString`. Now that you know that, you can go ahead and read its documentation to see which method to use for comparing it with other strings. –  Nov 23 '13 at 08:05
  • OK..Got it. Thanks H2CO3 and Kumar KI. I will change from NSValue to NSString. Thanks – Timothy Rajan Nov 23 '13 at 08:09

1 Answers1

0

Assuming value you keep there is NSString - the solution is to use:

if([[[NSUserDefaults standardUserDefaults] objectForKey:@"screenstatus"] isEqualToString:@"YOUR_STRING"])
{
     //value is equal
}

If under that key you are keeping different types than you can check if it's NSString using

[[[NSUserDefaults standardUserDefaults] objectForKey:@"screenstatus"]  isKindOfClass:[NSString class]]
Grzegorz Krukowski
  • 18,081
  • 5
  • 50
  • 71