Problem with date and time columns both bound to the same date attribute

  • I've found something that I think is a bug (and will file with Apple)
    but in the meantime I'd like to know if anyone has a reasonable
    workaround.  I've got a Core Data app and each entity has a date
    attribute.  I have a table that shows the entities with a column for
    each attribute.  I have a column for the date and another column for
    the time.  These are both bound to the same date attribute.  Each has
    a formatter that makes it so the date column displays the date and the
    time column displays the time.  The problem is, if you edit an
    entity's date by double clicking on the date column then entering a
    new date, the time gets reset to 00:00 and the same thing happens if
    you edit the time (date gets reset to 1/1/1970 or whatever).  I've
    posted a very simple example project here: http://www.andrewmadsen.com/misc/TimeAndDate.zip
      .  This project was created by making an incredibly simple (one
    entity with one attribute) Core Data model, dragging it into IB to
    create an interface, then adding a second column and setting up
    formatters so one column shows date, the other time.  I didn't write
    any code for it.  If you add an entity, set a date for it, then set a
    time, you'll immediately see that the date reverts back to Jan 1
    1970.  This is exactly the problem I'm describing.

    Anyway, I can't see why this behavior would be desired, so I think
    this is in fact a bug.  Has anyone seen this before and come up with a
    reasonably elegant workaround?

    Thanks,
    Andrew
  • On Dec 26, 2007, at 8:10 PM, Michael Babin wrote:
    >>
    >
    > As Matt wrote in his reply to you, this behavior may not actually
    > represent a bug (perhaps design limitation would be more
    > appropriate). An NSDateFormatter can convert from an NSDate to a
    > string, using the formatting information you specify, and from a
    > string to an NSDate, but it doesn't store the object value
    > represented and merge the changes back into the original object.
    > Given that, it might be possible to have the cell (which does have
    > the object value available) use the formatter to convert the string
    > into a date and merge that with the existing date value, but you
    > would need a specialized cell (not an NSTextFieldCell).
    >
    > A simpler approach would be to have your model expose separate "day"
    > and "time" properties that you could bind to for the separate
    > columns. You could store separate date properties in your entity, or
    > define getters and setters for each property that extract the
    > relevant portion of the date property and merge the changes back
    > into that date property.
    >
    > In terms of your sample project, you could define a TimeAndDate
    > class as follows (uses some Obj-C 2.0 features, such as properties
    > and dot syntax; substitute with ivars, getters/setters, and bracket
    > notation as needed/desired):
    > <snip>
    > For your example application, change your TimeAndDateEntity from
    > NSManagedObject to TimeAndDate in your data model and bind your
    > table columns to "day" and "time", respectively.
    >

    Thanks for the reply, doing it this way hadn't really occurred to me.
    I implemented day and time setters/getters that actually modify and
    return portions of the date attribute and it seems to be working
    great, thanks!  I did change the sort selector for both columns to
    "date" so that sorting would work properly as it did before.

    -Andrew
previous month december 2007 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
31            
Go to today