Getting the oldest/newest date of a column of dates

I have a table in which one column has NSDates. An Array controller manages the table. I want to get the oldest and newest dates in as fast and automatic fashion as possible (it must work smoothly for at least a thousand rows). I want to get that oldest/newest date regardless of which column is currently sorted.

One idea is to have an NSTextField bound to the array controller and use a Model Key Path and Display Pattern along the lines Shane described in his book page 38/39. But I simply cannot come up with a Model Key Path that would do what I want.

NSDate has a method compare: but that method is for comparing two dates, not an array of dates. (And the array controller does not implement compare:)

Any ideas of what I should do?
Any documentation on this?
Where can I read about which operations the ArrayController implements for Model Key Path?
If not doable this way, what other (fast) alternatives do I have?

Harald,

I’m not sure how you’re data is organized, but if you have an array of dictionaries (which is how you usually do multi column tables), you can access the minimum value of a particular key like so:

log theData’s valueForKeyPath_(“@min.theDate”)

Where theData is my array, and theDate is the key for the column with dates. Using @max.theDate will get you the last date.

You can also bind the values of text fields to the array controller with controller key “arranged objects” and a model key path of @min.theDate and @max.theDate.

Ric

Ahh!
Beautiful, Ric! Thanks!

It works perfect in both ways: (1) by assigning it to a property in the script and then binding that property to a label’s (i.e NSTextField’s) value (via Model Key Path) in the gui; (2) by binding the label to the array controller’s value or display pattern value (using only the string part of the command, i.e no valueForKeyPath:). I will use the first method since I will also need that value in another script object, so I will link it to this “original” property.

(I was a bit worried at first that my request for this feature would be a cpu hog, but this seems ultra-fast . what would I do without this forum:-))

As for understanding how it works and the possible set of opportunities: Apples “NSKeyValueCoding Protocol Reference” under “Constants” > “Array operators” lists some seemingly useful stuff. Are those the ones, and only those ones, that one can use in an array controller’s model key path for computing things, or in the valueForKeyPath_ method on arrays?

I’m pretty sure that those are the only ones. If you need something else, you’ll have to code it yourself

Ric

Good to know where to look. It was only after your answer I discovered that reference documentation, and then it all appeared a lot more clear to me. I had already tried various “functions” as the model key path, but I never tried “max” and “min” (which in hindsight looks pretty natural, but when I searched I couldn’t find such funtions in the places where I looked)

I have changed my mind on which method to use: it’s better to bind the values of the text fields to the array controller, since then they will always update automatically when the table changes. Then I bind the text fields also to properties in the script object so I can use the values elsewhere (which I need to do).

Best Regards,

In search for other things, I just saw in the documentation on Key-Value Coding Programming Guide under Collection Operators that they say in a note:

where a collection opertor is the middle item in: keypathToCollection.@collectionOperator.keyPathToProperty

So, it seems that one cannot code a custom operator.
In my case for this particular thread, I don’t need a custom operator, but I just wanted to post here what I found, since it might interest some people.