Possible Duplicate:
Why is my comparing if statement not working?
I am attempting to check to see if a particular setting was set to "On" or "Off". Here is my code:
BOOL switchOn;
if ([self loadSettings:@"useLocation"] == @"On"){
switchOn = YES;
NSLog(@"Use Location (Yes): %@",[self loadSettings:@"useLocation"]);
}else{
switchOn = NO;
NSLog(@"Use Location (No): %@",[self loadSettings:@"useLocation"]);
}
NSLog(@"Switch On: %@",switchOn ? @"YES":@"NO");
Printed to the log is:
Use Location (No): On
Switch On: NO
You can see that for the "Use Location (No)" to come up, the function should not have returned "On". But it did return "On". Am I not comparing them correctly?
Here is my code for loading the defaults:
- (NSString *)loadSettings: (NSString *)key{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults synchronize]; // this method is optional
// Get the results out
NSString *results = [defaults stringForKey:key];
return results;
}
I'm returning an NSString
, so I'm really not sure what the issue is. I get no errors at all in the build environment before or after compiling.