Caveat Formatter

from blog The Desolation of Blog, | ↗ original
NSFormatter is intended for subclassing. You know what they say, the road to Radar is paved with good intentions. A formatter is particularly useful if you need to restrict input to a text field as the user types. Below I've written a sample NSFormatter subclass that accepts only decimal digits. For the sake of brevity, I've provided simplistic implementations of the required -getObjectValue:forString:errorDescription: and -stringForObjectValue: overrides; depending on your needs, you may want NSNumber as the object instead of NSString. The purpose of this blog post is to point out a caveat with validating partial strings, so let's focus on the final method, which I'll call -isPartialStringValid: for short, because the full method signature is overly long. Also known as "Part-y" among good friends, this method isn't one of those who whine when you give them a nickname.