NSPredicate在NSString类型的两个日期之间进行过滤 [英] NSPredicate to filter between two dates of NSString type

查看:132
本文介绍了NSPredicate在NSString类型的两个日期之间进行过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据(在 NSMutableArray 中为 NSDictionary ),示例数据如下,

I've some data (as NSDictionary in an NSMutableArray), a sample data is like,

将每一行视为 NSDictionary ,整个表格为 NSMutableArray 包含事件。

Consider each row as NSDictionary and entire table is an NSMutableArray contains events.

我想在两个日期之间产生结果,所以我使用 NSPredicate 来过滤我的数据。

I want to result between two dates, so I'm using NSPredicate to filter my data.

这是我的代码片段,

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Date >=  %@ AND
Date <=  %@", startDate,endDate];

NSArray *filter = [arrayEvents filteredArrayUsingPredicate:predicate];

测试过滤

案例1:

NSString *startDate = @"01/07/14";
NSString *endDate = @"31/07/2014";

输出:
所有事件(行)。

Output: All events (rows).

案例2:

NSString *startDate = @"20/07/14";
NSString *endDate = @"31/07/2014";

输出:
最后四个事件(行)。

Output: Last four events (rows).

案例3:

NSString *startDate = @"11/07/14";
NSString *endDate = @"20/07/2014";

输出:
只有第二个事件(行)。

Output: Only 2nd event (row).

案例4:

NSString *startDate = @"01/08/14";
NSString *endDate = @"31/08/2014";

输出:
所有事件(行)。

Output: All events (rows).

结论:


案例1 ,2和3是正确的,其中案例4不正确。

Case 1, 2 and 3 is correct, where Case 4 is incorrect.






注释:


  • 日期类型为 NSString

  • 日期格式是日期 - 月 - 年(即年/月/日)

  • 相同结果甚至日期格式为 - 连字符而不是 /(正斜杠)

  • 即使我们更改日期格式月 - 日 - 年,结果相同(即MM / dd / yyyy)

  • Date is of type NSString
  • Date format is date-month-year (i.e. dd/MM/yyyy)
  • Same result even date is formatted with - hyphen instead of / (forward slash)
  • Same result even if we changed date format month-date-year (i.e. MM/dd/yyyy)

有人请帮助我,我在这里做错了什么?我缺少什么?我知道其他几个问题,但没有一个问题对我有帮助。

Some one please help me what I am doing wrong here? Anything that I'm missing? I know there's few other questions but none of which helped me.

推荐答案

您需要将数据类型更改为日期(即使是这里最有意义的)。
您的测试按时间传递,因为它们完全匹配一个字符串,而您的谓词包含等号。
所以如果你只是查询<并且>每个测试都不会返回任何结果

You need to change your datatype to date (what even makes most sense here). Your tests pass by time, cause they exactly match one string and your predicate contains equals. So if you would just query for < and > no result would be returned in each "test"

然后你的代码段

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Date >=  %@ AND
Date <=  %@", startDate,endDate];

NSArray *filter = [arrayEvents filteredArrayUsingPredicate:predicate];

按预期工作。没有办法用字符串做,因为它需要一个日期格式化程序来解析字符串中的日期并进行比较。这绝对是低效率的。
因此,如果您真的想要过滤它,请创建一个NSDateFormatter实例并运行每个Dictionary。删除日期并将新格式化的日期与开始日期和结束日期进行比较。如果是在它之间将它添加到结果数组(也需要在之前创建它)。

works as expected. There is no way to do that with strings as it would require a date formatter to parse the date from the string and compare that. This would be absolutely inefficient. So if you really want to filter this, create a instance of NSDateFormatter and run through each Dictionary. Grab out the date and compare that new formatted date to your start and end date. If it is in between add it to the result array (also need to create this before).

NSMutableArray* result = [NSMutableArray array];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"dd/MM/yy";
for (NSDictionary *dict in yourArray) {
    NSString *dateString = dict[@"Date"];
    NSDate *date = [formatter dateFromString:dateString];
    if ([date compare:startDate] > 0 && [date compare:endDate] < 0) {
       [result add:dict];
    }
}

//在此使用过滤后的数组

//use the filtered array here

这篇关于NSPredicate在NSString类型的两个日期之间进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆