NSPredicateEditorRowTemplate for date comparison
Asked Answered
N

2

7

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

Nochur answered 10/3, 2010 at 21:12 Comment(0)
N
8

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:

Nochur answered 12/3, 2010 at 22:25 Comment(0)
T
0

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.

Territorialize answered 24/8, 2010 at 21:44 Comment(3)
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.Nochur
Sure. I'm a StackOverflow newbie. How do I email you?Territorialize
my profile has a link to my homepage, which has an email form.Nochur

© 2022 - 2024 — McMap. All rights reserved.