当我尝试传递字符串时谓词不起作用

时间:2014-09-26 14:38:14

标签: ios objective-c

它的工作:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"dateSave ==[c] %@",[[UtilityClass   dateComponentsFromDate:[NSDate date]] valueForKey:@"date"]];

但我需要抓取:

str_predicate=[NSString stringWithFormat:@"%@ AND hourSave == %d",str_predicate, [[[[self.dict_ChallengesAccepted  valueForKey:@"ChallengesAccepted"]objectAtIndex:indexPath.row]valueForKey:@"accept_time"]  intValue]+diff_Hour];

str_predicate=[NSString stringWithFormat:@"%@ dateSave == %@",[str_predicate  stringByReplacingOccurrencesOfString:@"1 AND "
                                                                                                            withString:@""],[[UtilityClass dateComponentsFromDate:[NSDate date]] valueForKey:@"date"]];
[NSPredicate predicateWithFormat:str_predicate]

Predicate Log = dateSave == [c]" 2014-09-26" AND hourSave == [c]" 18" AND hourSave hourSave == [c]" 19"

如果我只通过dateSave == [c]" 2014-09-26"那么它的返回数据

但是当我尝试传递dateSave == [c]" 2014-09-26" AND hourSave == [c]" 18" AND hourSave hourSave == [c]" 19"返回0

但在我的数据库中有18和18的数据。 19小时插槽

我能够解决它:

我的更新代码是:

int hourDiff=[[[NSDictionary dictionaryWithDictionary:[UtilityClass dateComponentsFromDate: [NSDate date]]]valueForKey:@"hour"] intValue]-[[[[self.dict_ChallengesAccepted  valueForKey:@"ChallengesAccepted"]objectAtIndex:indexPath.row]valueForKey:@"accept_time"]intValue];
 for (int diff_Hour=0; diff_Hour<=hourDiff; diff_Hour++) {

            [predicatesArray addObject:[NSPredicate predicateWithFormat:@"hourSave ==[c] %d", [[[[self.dict_ChallengesAccepted  valueForKey:@"ChallengesAccepted"]objectAtIndex:indexPath.row]valueForKey:@"accept_time"]   intValue]+diff_Hour]];
}
predicate=[NSCompoundPredicate orPredicateWithSubpredicates:predicatesArray];

         NSArray *searchPredicatesArray = [NSArray arrayWithObjects:[NSPredicate  predicateWithFormat:@"dateSave ==[c] %@",[[[self.dict_ChallengesAccepted  valueForKey:@"ChallengesAccepted"]objectAtIndex:indexPath.row]valueForKey:@"accept_date"]],predicate,     nil];
predicate=[NSCompoundPredicate andPredicateWithSubpredicates:searchPredicatesArray];

1 个答案:

答案 0 :(得分:0)

好吧,如果某事等于18,则它不能等于19,所以你提供了错误的表达。你应该使用&#34; OR&#34;和括号。你的最终表达应如下所示:

dateSave ==[c] "2014-09-26" AND (hourSave ==[c] "18" OR hourSave ==[c] "19")