When storing objects in the NSUserDefaults
or in some other key-value store, convenience methods are often used to store and retrieve keys.
boolForKey:
is such a method, but since the BOOL
data types have only two values, the the information, if the key was in the store or not, is lost in this method. Because it returns NO if the value NO was stored and it also returns NO if no information was stored at all.
This is the reason, why I prefer to store “default-off” values in the user defaults. I rather check for disclaimerWasRead
than for shouldShowDisclaimer
. I rather check for values which can be off or non-existing by default and will be YES for exactly one decision than having distinguish all three states.
[Edit:] A similar situation arises if you use integerForKey:
. The value 0 does not inform you, if the key existed at all, or if it existed as 0. If you use objectForKey:
first and then transform the result to a integer value, than you still can distinguish if you received nil from the first call.[End Edit]