Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSPredicateEditorRowTemplate for date comparison

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).

like image 756
Dave DeLong Avatar asked Mar 10 '10 21:03

Dave DeLong


1 Answers

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.

Edit 24 Nov 2010

For anyone who comes across this again, I've written a couple blog posts on creating custom NSPredicateEditorRowTemplates:

  • Creating a simple NSPredicateEditorRowTemplate
  • Creating an advanced NSPredicateEditorRowTemplate (this one shows how to do the inTheLast and inTheNext row template)
like image 147
Dave DeLong Avatar answered Oct 13 '22 03:10

Dave DeLong