Binding NSTextField to a number through NSArrayController

  • 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?

    Thanks,

    Todd

    Here is my binding settings for the NSTextField

    Bind to: procArrayCtlr (an NSArrayController)
    Controller Key: selection
    Model Key Path: recordCount  <-- this gets red exclamation mark
    Value Transformer: (empty right now)
  • 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.
  • IIRC that's just telling you that IB can't validate your keypath. (There's a place in one of the inspectors to define what the "known" keypaths are.)

    NSValueTransformer is generally not applicable for numbers, but it's usually good practice to attach an NSNumberFormatter to the field to handle both display formatting and input validation (e.g. min/max, etc.)

    (Sent from my iPad.)

    --
    Conrad Shultz

    On Jun 30, 2012, at 12:00, Todd Heberlein <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?
    >
    >
    > Thanks,
    >
    > Todd
    >
    > Here is my binding settings for the NSTextField
    >
    > Bind to: procArrayCtlr (an NSArrayController)
    > Controller Key: selection
    > Model Key Path: recordCount  <-- this gets red exclamation mark
    > Value Transformer: (empty right now)
previous month june 2012 next month
MTWTFSS
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30  
Go to today