views:

286

answers:

2

Hello everyone,

How would I check in a "if" statement if the NSUserDefault object is saved there or not? I'm not really sure how to call it.. So a pretty short question..

Thanks

+5  A: 
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"Foo"] != nil) {
  NSLog(@"an object is saved under \"Foo\"!");
}
Dave DeLong
+1  A: 

Dave's answer is correct, but I'd skip the explicit test for nil:

if ([[NSUserDefaults standardUserDefaults] objectForKey:@"Foo"]) {
    NSLog(@"An object is saved under \"Foo\"!");
}
Abizern
It's a style choice. I like to be explicit in my comparisons :)
Dave DeLong
@Dave DeLong: Absolutely. Mine is a style choice as well, just pointing out an alternative. Consistency is more important. :)
Abizern