views:

654

answers:

2

I'm building an NSPredicateEditor, and I want the ability to do advanced date comparison.

I realize that I can build an NSPredicateEditorRowTemplate with a rightExpressionType of NSDateAttributeType, but the predicates I want to build need to be much more advanced than that.

For example, I need to basic comparison like:

  • dateKeypath < aDate
  • dateKeypath <= aDate
  • dateKeypath = aDate
  • dateKeypath != aDate
  • dateKeypath > aDate
  • dateKeypath >= aDate

These basic comparisons are quite easy to achieve, and I have these working. However, I also need to do comparisons like:

  • dateKeypath isInTheLast n days (or weeks, months, years)
  • dateKeypath isNotInTheLast n days (or weeks, months, years)
  • dateKeypath between aDate and anotherDate

How can I achieve these sorts of comparisons? I understand that I'll need to create a custom NSPredicateEditorRowTemplate, but I haven't found any clear documentation on how to achieve something like this.

EDIT Bonus points are available for also knowing how to make these comparisons a full date-time (year-month-day-hour-minute-second) comparison (as NSDateAttributeType only has year-month-day granularity).

+5  A: 

In retrospect, this answer seems somewhat obvious:

I actually described 3 predicate editor row templates, not one. The three templates are:

  • dateKeyPath [<, <=, =, >, >=, !=] [NSDatePicker]
  • dateKeyPath [inTheLast, notInTheLast] [NSTextField] [NSPopUpButton]
  • dateKeyPath [between] [NSDatePicker] "and" [NSDatePicker]

If you build these three predicate editor row templates and give them to the predicateEditor, the editor will realize that they're all using the same dateKeyPath, and display then crunch all their operators into a single popup button. It will then switch out the actual views to the right of the operator depending on which operator is selected.

Edit

For bonus points: you can modify the precision of the date pickers by overriding the templateViews method, retrieving the templateViews from super, and setting the datePickerElements on the appropriate pickers.

Dave DeLong
A: 

I don't think that the second predicate template will work as expected. The problem is that you need a relative date - a date that is evaluated relative to the current date.

I have implemented such a predicate editor template (with all the associated code) based on some code (and advice) from an Apple engineer. If anyone is interested I can provide the details, etc. My implementation is quite involved.

Martin Stanley
Yeah, I eventually worked that as well. Shoot me an email with what you came up with! I'd love to share notes with you.
Dave DeLong
Sure. I'm a StackOverflow newbie. How do I email you?
Martin Stanley
@Martin my profile has a link to my homepage, which has an email form.
Dave DeLong