views:

55

answers:

1

In one app I am working on I need to let the user edit a list of dates. I have those NSDate objects in an array and bound that to a NSArrayController. I bound a NSTableColumn (the only column in a table view) to that array controller using the key path arrangedObjects.self. This works fine - I get all the dates displayed in the table just the way I want it and I can add and remove dates.

The trouble is I can not edit the dates in the table. If I try I get an exception telling me, that NSDate is not key-value-coding compliant to the key self, which of course is true, there is no setSelf: method.

I have two ideas how I could solve this:

  1. have my array not store NSDate directly but put it in an NSMutableDictionary or some other object first.
  2. not allow the user to edit the date directly but have him remove the wrong one and re-add the corrected date.

I don’t really like both solutions. I don’t like the first because I have to put stuff in my model just to get the GUI right. And the second is not very user-friendly.

So does anyone have an idea if there is a third and even better way to solve this?

Update: Temporary solution

For now I am using the table view’s data source to deal with this instead of using bindings. But I’d still like to see if there is a better solution using bindings for that problem, since I don’t really want to write a data source every time this (or a similar problem) turns up. It happens not just with NSDate but all other value type classes provided by cocoa, even if they are mutable (like NSMutableString).

+1  A: 
Joshua Nozzi
Of course I am binding to the column, and I am using a `NSDateFormatter`. But I’ll try leaving the `self` out of the key path.
Sven
You say "of course" as if it couldn't be any other way. It can. :-) Tables themselves can be bound and people forget to use formatters. We had no way of knowing because you didn't specify in your first revision. In any case, a model key path of self is wrong. The arranged object at a given index IS itself.
Joshua Nozzi
Even if I am leaving out the `self` part of the key path it won’t work. After editing a date I get an error message that `__NSCFDate` is not key value coding-compliant for the key `.`
Sven
Bah. My apologies - I've never tried loading NSDate objects into an array controller's content array before. Even with a date formatter, this does not work. Your initial thought to enclose it within an NSMutableDictionary does seem to work happily with editing. I believe this is because you can't modify a date. You create a date and if you wish to change it, you replace it with another. Therefore, it must be wrapped in either something's property or thrown in a dictionary. Updating my answer to reflect this.
Joshua Nozzi
( Up-voted the question because it was a fun quiz I got wrong at first. :-) )
Joshua Nozzi