Wy does NSUserDefaults objectForKey method, returns a string with “0” and not nil?
Documentation states,
Return Value
The object associated with the specified key, or nil if the key was
not found.
NSDate * date = [[NSUserDefaults standardUserDefaults] objectForKey:OVERTIME_RATE_MARKER];
if (date) {
//something
}
The conditional statement above, always executes, because if the object is not found, it’s returning an object of class: __NSCFString with “0”. When it should be returning “nil”
I can of course, test the string for “0”. But I don’t really want to do that.
I also tried to use ID for type. I keep getting a __NSCFString with “0”.
Can anyone help me figure this out?
Any help is appreciated!
Thank you
This API definitely returns
nilwhen the key doesn’t exist. Either the key was already set in your preferences, or you’re setting it to@"0"somewhere else in your code before this code that reads it.