NSTextField binding returns nil not empty string

  • What is the best way to handle this? I have a table view (using cell views) which has multiple text fields in it. I have an arrayController that holds an array of the custom objects that hold the data for each tableCellView (row). The array is made in the appDelegate and it always consists of text or empty text strings. It all works great as described.

    When I edit the text in the fields, if I remove all the text from one field, it must be returning nil back to the bound object. This messes things up later, because I need to return an empty string for each of the ivars in my custom object, not nil.

    So, my way to get around this was to essentially check if the object's ivar was nil, and if so then return empty string when called upon. This works just fine and I think it's efficient enough.

    But is there an even better way to handle that? I tried using "null placeholder" in the IB/binding options but that didn't seem to work. The ivars for the custom object aren't set to "strong" and I tried that but that didn't seem to help either. Is there anything I'm missing or does an NSTextField always return "nil" when a value is removed from it, or can I force it to return an empty string?

    -------custom object (with ARC):
    @interface SpecialObject : NSObject
    @property(nonatomic, readwrite) NSString*headline;
    ...

    Thanks, Chris
  • On Apr 30, 2013, at 1:14 PM, Chris Paveglio wrote:

    > What is the best way to handle this? I have a table view (using cell views) which has multiple text fields in it. I have an arrayController that holds an array of the custom objects that hold the data for each tableCellView (row). The array is made in the appDelegate and it always consists of text or empty text strings. It all works great as described.
    >
    > When I edit the text in the fields, if I remove all the text from one field, it must be returning nil back to the bound object. This messes things up later, because I need to return an empty string for each of the ivars in my custom object, not nil.
    >
    > So, my way to get around this was to essentially check if the object's ivar was nil, and if so then return empty string when called upon. This works just fine and I think it's efficient enough.
    >
    > But is there an even better way to handle that? I tried using "null placeholder" in the IB/binding options but that didn't seem to work. The ivars for the custom object aren't set to "strong" and I tried that but that didn't seem to help either. Is there anything I'm missing or does an NSTextField always return "nil" when a value is removed from it, or can I force it to return an empty string?

    Yes, NSTextField has (IMHO) an imperfect understanding of the difference between nil and empty string. I feel your pain.

    A slightly better approach might be to use key-value validation, as such:

    -(BOOL)validateHeadline:(id *)ioValue error:(NSError **)outError
    {
    if ( *ioValue == nil )
      *ioValue = @"";
    return YES;
    }

    This way you don't add side-effects to your accessors, and avoid hard to debug issues if you happen to use the ivar directly somewhere down the road.

    HTH,

    Keary Suska
    Esoteritech, Inc.
    "Demystifying technology for your home or business"
previous month april 2013 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