NSPredicateEditorRowTemplate 用于日期比较
我正在构建一个 NSPredicateEditor
,并且我希望能够进行高级日期比较。
我意识到我可以使用 NSDateAttributeType
的 rightExpressionType
构建一个 NSPredicateEditorRowTemplate
,但是我想要构建的谓词需要比这更高级。
例如,我需要进行基本比较,例如:
dateKeypath < aDate
- dateKeypath <= aDate
dateKeypath = aDate
dateKeypath != aDate
dateKeypath > aDate
dateKeypath >= aDate
这些基本比较很容易实现,并且我已经完成了这些工作。但是,我还需要进行比较,例如:
dateKeypath isInTheLast n days
(或周、月、年)dateKeypath isNotInTheLast n days
(或周、月、年)aDate 和 anotherDate 之间的 dateKeypath
我怎样才能实现这些类型的比较?我知道我需要创建一个自定义 NSPredicateEditorRowTemplate
,但我还没有找到任何关于如何实现此类目标的明确文档。
编辑 如果还知道如何使这些比较成为完整的日期时间(年-月-日-时-分-秒)比较(因为 NSDateAttributeType
仅具有年-月-日粒度),则可以获得奖励积分。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
回想起来,这个答案似乎有些明显:
我实际上描述了 3 个谓词编辑器行模板,而不是一个。这三个模板是:
dateKeyPath [<, <=, =, >, >=, !=] [NSDatePicker]
dateKeyPath [ Between] [NSDatePicker] "and" [NSDatePicker]
如果你构建这三个谓词编辑器行模板并将它们交给 predicateEditor,编辑器会意识到它们都是使用相同的
dateKeyPath
,并显示然后将所有运算符压缩到一个弹出按钮中。然后,它将根据选择的操作员将实际视图切换到操作员右侧。编辑
奖励积分:您可以通过重写
templateViews
方法、从super
检索 templateViews 并设置适当的选择器上的datePickerElements
。2010 年 11 月 24 日编辑
对于再次遇到此问题的人,我写了几篇关于创建自定义
NSPredicateEditorRowTemplates
的博客文章:NSPredicateEditorRowTemplate
NSPredicateEditorRowTemplate
(此示例展示了如何执行inTheLast< /code> 和
inTheNext
行模板)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 fromsuper
, and setting thedatePickerElements
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
:NSPredicateEditorRowTemplate
NSPredicateEditorRowTemplate
(this one shows how to do theinTheLast
andinTheNext
row template)我认为第二个谓词模板不会按预期工作。问题是您需要一个相对日期 - 相对于当前日期计算的日期。
我已经根据苹果工程师的一些代码(和建议)实现了这样的谓词编辑器模板(以及所有相关的代码)。如果有人感兴趣,我可以提供详细信息等。我的实现非常复杂。
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.