Re: Bindings and empty text field nil assigments
Re: Bindings and empty text field nil assigments
- Subject: Re: Bindings and empty text field nil assigments
- From: mmalc crawford <email@hidden>
- Date: Wed, 27 Aug 2008 21:32:17 -0700
On Aug 27, 2008, at 9:00 PM, Markus Spoettl wrote:
I have objects storing simple scalar values (NSInteger, double).
Those values/properties are bound to text fields. Now, if the user
clears the text field and the underlying property gets updated, it's
not simply setting the value to 0 or its equivalent, instead
- (void)setNilValueForKey:(NSString *)key
is invoked. Failing to implement it causes an exception when exiting
out of a cleared text field bound to a scalar value.
This is expected and desirable behaviour.
I was wondering if there was a more direct and less glue-code
involving way of doing this.
This isn't "glue code", this is "business logic". It's not possible
to determine a priori how a scalar value should represent null, so you
need to implement this method to tell your model objects how you want
them to represent one.
I don't want to go and store NSNumbers instead of scalar values,
which I imagine would have the advantage of accepting nil values
inherently.
Why not? There may be an advantage if you are doing a lot of
mathematical calculations, but the disadvantage is that you have to
convert between object and scalar values for the user interface. If
you haven't already, you should profile your application to be sure
you're not prematurely optimising.
Can I tell the bindings system to directly set scalars to 0 in case
of nil values?
Not directly. You could implement a value transformer to turn a nil
value into a number 0, or (best of all) you could use a formatter to
prevent a user from entering a nil in the first place...
mmalc
_______________________________________________
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