Question
Why was the Swift SDK designed such that if I do this:
someDictionaryObject.int(forKey: "foo")
And foo does not exist or is not numeric, 0 is returned instead of a nil? This makes it impossible to distinguish between:
“I actually stored 0 for this property” and “This property was either missing or something entirely incorrect, such as a String, that I should handle.”
Context
I can solve the “does foo exist at all?” problem for DictionaryObject by using contains(key: "foo") But for ArrayObject where I’m iterating indices, I no longer have any way of distinguishing between “the property was actually 0” and every other case that automatically becomes zero.
It’s as if we took Date and, if the object that was supposed to be a Date is invalid, we just returned a Date set to 1 January 1970. Now we’d be unable to answer the question: “Is that the real date, or is it the error marker?”
API Improvement
I realize this behavior is now baked in, so it can’t be changed. But could we get an improved, alternate API alongside it? Something where if I ask for a scalar Int, Bool, Double, etc and the thing in the database is NOT, in fact, that kind of value, I get back nil so I can react appropriately?