编写一个简单的 NSpredicate - 逻辑问题 [英] Writing a simple NSpredicate - Logic Issue

查看:47
本文介绍了编写一个简单的 NSpredicate - 逻辑问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Person 对象.而这个 person 对象具有以下属性;

I have a Person object. and this person object has the following attributes;

Name
StartDate
EndDate

我正在将这个 Person 对象保存到一个 array.这个数组可能包含超过100个人对象.

I am saving this Person objects to an array. This array might contain more than 100 person objects.

以下是该数组中 person 对象的示例;

The following is an example of person objects in that array;

John, Tue Feb 22, Thr Mar 30
Jack, Wed Mar 09, Fri Apr 21
Jack, Thu Mar 19, Fri Dec 20
Jack, Tue Jan 08, Fri Apr 26 etc..

现在我需要提供一个日期,例如 Wed 29 Mar,我需要检查它是否在 StartDateEndDate 的范围内persons 对象数组中.我怎样才能做到这一点 ?我的工作如下,但它不起作用(它给出了错误的结果)

Now i need to will supply a date, say for example Wed 29 Mar, and i need to check if it's in the range of StartDate and EndDate in the array of persons object. How can i do this ? My working are as follows, but it doesn't work (It gives incorrect results)

NSPredicate *datePred = [NSPredicate predicateWithFormat:@"StartDate>= %@ && EndDate<= %@",givenDate,givenDate];
            resultArray = [arrayContainingAllPersonObjects filteredArrayUsingPredicate:datePred ];

我也尝试了以下方法,但它也给出了错误的结果(已从 SO 帖子中删除);

I also tried the following method, but it too gives incorrect results (which was taken off a SO post);

NSPredicate *greaterThanPredicate = [NSPredicate predicateWithFormat:@"StartDate<= %@", providedDate"];
NSPredicate *lessThanPredicate = [NSPredicate predicateWithFormat:@"EndDate>= %@", ProvidedDate"];
NSPredicate *betweenPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:greaterThanPredicate, lessThanPredicate, nil]];

我不知道是什么导致了这个,帮助

I have no clue as in what is causing this, Help

推荐答案

尝试使用 '%@' like StartDate = '%@' &&EndDate ='%@'

Try using '%@' like StartDate = '%@' && EndDate ='%@'

更新:

试试

NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"StartDate = '%@', startDateHere]];

这篇关于编写一个简单的 NSpredicate - 逻辑问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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