Re: Binding NSTextField to a number through NSArrayController
Re: Binding NSTextField to a number through NSArrayController
- Subject: Re: Binding NSTextField to a number through NSArrayController
- From: Quincey Morris <email@hidden>
- Date: Sat, 30 Jun 2012 12:28:52 -0700
On Jun 30, 2012, at 12:00 , Todd Heberlein wrote:
> I have an NSTextField that I am binding to an NSArrayController's selection (a number value in that selection). In the "Model Key Path" field I get an exclamation mark inside a red stop sign icon.
>
> Even more confusingly, the code still works. I get the right value! But that exclamation mark has be nervous (like it works now, but no guarantee).
>
> What am I doing wrong? Do I need to do something special when binding an NSTextField to an NSNumber or NSInteger? Do I need to venture into the world of NSValueTransformers whenever I bind an NSTextField to anything other than an NSString?
Sometimes, IB's model key warning icons are inscrutable and you just have to ignore them. That's not to say it's complaining unnecessarily, but if you can't figure out what it's complaining about, you've got no choice but to let the run time throw an exception if it doesn't like what you did.
In this case, though, there is something you can fix. You should add a NSNumberFormatter to the text field.
When there's no number formatter, the object value passed through the bindings mechanism is a NSString. When there is a number formatter, the object is a NSNumber. (When there is a date formatter, the object is a NSDate, of course.)
Because NSString and NSNumber both respond to 'intValue', either works to transfer a scalar model value to/from a text field, but only in simple cases. (For example, since NSString doesn't respond to 'unsignedIntValue', it might not work for an unsigned scalar model value.)
My guess is that this is what IB is complaining about, and using a NSNumberFormatter will solve it. As a side benefit, you get to actually format the number (e.g. displaying thousands separators) if you wish.
_______________________________________________
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