1

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

lab12
  • 6,400
  • 21
  • 68
  • 106

2 Answers2

7
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"Foo"] != nil) {
  NSLog(@"an object is saved under \"Foo\"!");
}
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
4

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
  • 146,289
  • 39
  • 203
  • 257