max(arrayOfNSDates) 的 NSPredicate

发布于 2024-12-06 20:09:53 字数 671 浏览 0 评论 0原文

我遇到了 NSPredicate 问题。

我有一些引擎,其中谓词是根据来自 Web 服务的定义构建的。

我使用类似的东西:

[NSPredicate predicateWithFormat:@"max({1,2,3})==3"]

它很棒,但我需要 NSDate 对象的 max 函数。 这里 http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSExpression_Class/Reference/NSExpression.html#//apple_ref/doc/uid/TP30001190 写道 max() 函数只能获取包含表示数字的对象的数组。

有没有任何解决方案可以使用此函数(覆盖某些方法,为 NSPredicate 创建某些类别)

提前致谢。

I got problem with NSPredicate.

I got some engine where the predicates are constructed from a definition coming from a web service.

I use somethning like that:

[NSPredicate predicateWithFormat:@"max({1,2,3})==3"]

it's great, but I need max function for NSDate objects.
Here http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSExpression_Class/Reference/NSExpression.html#//apple_ref/doc/uid/TP30001190 is wrote that max() function can get only array containings objects representing numbers.

Is there any solution to use this function for that (override some method, create some category for NSPredicate)

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜吻♂芭芘 2024-12-13 20:09:53

好吧,这是可能的,但是很奇怪。我们必须以编程方式构建格式字符串。或者,我们可以通过创建单独的 NSExpression 对象来构建这一切,但这会需要更多代码(尽管可能更安全,但无论如何)。

NSDate *maxDate = ...; 
NSArray *arrayOfDates = ...; // an NSArray of NSDate objects

NSMutableArray *dateFormats = [NSMutableArray array];
for (NSDate *date in arrayOfDates) {
  NSString *format = [NSString stringWithFormat:@"%f", [date timeIntervalSinceReferenceDate]];
  [dateFormats addObject:format];
}

NSString *dateFormatString = [dateFormats componentsJoinedByString:@","];
NSString *format = [NSString stringWithFormat:@"CAST(max({%@}) = %%@, 'NSDate')", dateFormatString];

// format now looks like CAST(max({xxxx.xxxx, nnnn.nnnn, aaaa.aaaa, ....}), 'NSDate') = %@

NSPredicate *datePredicate = [NSPredicate predicateWithFormat:format, maxDate];

这里的技巧是使用自参考日期以来日期的绝对时间间隔作为 max() 函数的参数,然后将 that 的结果转回日期通过使用CAST()。有关如何对日期使用 CAST() 函数的更多信息,请参阅此博文。

OK, this is possible, but it's weird. We'll have to build the format string programmatically. Alternatively, we could built it all by creating the individual NSExpression objects, but that'd be a lot more code (albeit likely safer, but whatever).

NSDate *maxDate = ...; 
NSArray *arrayOfDates = ...; // an NSArray of NSDate objects

NSMutableArray *dateFormats = [NSMutableArray array];
for (NSDate *date in arrayOfDates) {
  NSString *format = [NSString stringWithFormat:@"%f", [date timeIntervalSinceReferenceDate]];
  [dateFormats addObject:format];
}

NSString *dateFormatString = [dateFormats componentsJoinedByString:@","];
NSString *format = [NSString stringWithFormat:@"CAST(max({%@}) = %%@, 'NSDate')", dateFormatString];

// format now looks like CAST(max({xxxx.xxxx, nnnn.nnnn, aaaa.aaaa, ....}), 'NSDate') = %@

NSPredicate *datePredicate = [NSPredicate predicateWithFormat:format, maxDate];

The trick here is to use the date's absolute time interval since the reference date as the arguments to the max() function, and then turn the result of that back into a date by using CAST(). More info on how to use the CAST() function with dates can be found in this blog post.

打小就很酷 2024-12-13 20:09:53

这个解决方案确实很好,但没有解决我的问题。
我得到这样的信息:

NSPredicate* predicate = [NSPredicate predicateWithFormat: condition];
[predicate evaluateWithObject:nil substitutionVariables: currentForm];

并且条件可以是:

max($widgetWhichReturnDate.result.result,$widgetWhichReturnDate.result) 

或者

max($widgetWhichReturnInt.result,$widgetWhichReturnInt1.result)

这里使用什么小部件取决于网络服务。
所以我不喜欢对 Date 和 int 使用不同的函数(或者将来可能使用 float、double 等)

This solutions is realy good, but don't solve my problem.
I got something like this:

NSPredicate* predicate = [NSPredicate predicateWithFormat: condition];
[predicate evaluateWithObject:nil substitutionVariables: currentForm];

And condition can be:

max($widgetWhichReturnDate.result.result,$widgetWhichReturnDate.result) 

or

max($widgetWhichReturnInt.result,$widgetWhichReturnInt1.result)

what widget is used here depends from webservice.
So I don't like to use different functions for Date and int (or in the future maybe float, double etc)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文