Re: Number Formatter Default Value (or combo box?)
Re: Number Formatter Default Value (or combo box?)
- Subject: Re: Number Formatter Default Value (or combo box?)
- From: Quincey Morris <email@hidden>
- Date: Fri, 15 Mar 2013 18:38:20 -0700
On Mar 15, 2013, at 18:07 , Seth Willits <email@hidden> wrote:
> So my questions:
>
> 1) I see no way for a combo box to have a "default value" if say the formatter returns nil
> 2) I see no way for a formatter to have a "default value" if it can't convert a value.
>
>
> Am I wrong on that? I don't think I am, so I guess I'll keep on trucking with the above code.
The only thing that springs to mind is that *perhaps* it's more correct to think of the formatter as a validator of input structure, rather than of input values. From that point of view, it's not entirely clear that it ought to be responsible for providing a default value -- or, similarly, to clamp the input to a minimum or maximum value.
To clarify what I mean, let's suppose that the input field is bound to an ordinary KVC property in your data model, of type NSNumber. You'd probably want to have a check (in either a validator or setter method, or both) for a nil value, in order to reject it or to replace it with the default value. That's because you likely don't want to restrict yourself -- in general -- to the design assumption that a validated input field is the only source of property value changes.
In your case, you've basically moved the KVC validator method code into the number formatter. Given that you're binding to NSUserDefaults directly, that seems like a reasonable simplification, at the cost of *very* slightly abusing the MVC pattern. The alternative would be to create your own data model properties to act as intermediary between NSUserDefaults and the rest of your app, providing KVC validation at the data model end, but that probably seems like overkill for the relatively straightforward validation you want to do.
FWIW, my personal preference is never to bind directly to NSUserDefaults, for exactly this kind of reason. I prefer to put boilerplate code (into, say, the app delegate) to do the mediation, so that I can intervene into the touching of user defaults as and when required.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden