I set a BOOL value in NSUserDefaults as follows.
[userDefaults setBool:NO forKey:@"theKeyOfMyBOOL"];
Somewhere else in my code, I want to perform some action if the key “theKeyOfMyBOOL” is not set in NSUserDefaults. How do I verify whether this key was set? If I do
[userDefaults boolForKey:@"theKeyOfMyBOOL"];
then the value returned could be nil, which evaluates to false I believe, or the actual BOOL value of NO, which values to false.
However, I need to differentiate between these two values (as well as YES of course). How do I reliably do that?
[userDefaults boolForKey:@"theKeyOfMyBOOL"];returns a BOOL, so eitherYESorNO(notnil).Internally, it is stored as an
NSNumber. So, if you callyou will be given an
NSNumber, if you have ever stored anything, ornil, if you have not.