2014-09-26 15 views
0

其工作:謂詞不工作時,我試圖通過串

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] 

謂語登錄= dateSave == [C]「2014年9月26日「AND hourSave ==並[c] 」18「 AND hourSave hourSave ==並[c] 」19「

如果我只通過dateSave ==並[c] 」2014年9月26日「,那麼它的返回數據

但是當我嘗試通過dateSave == [c]「2014-09-26」AND hourSave == [c]「18」AND hourSave hourSave == [c]「19」return 0

但在我的數據庫有數據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]; 
+0

我能夠成功解決我的問題。以下是我更新的代碼。 – 2014-09-26 15:55:12

回答

0

好吧,如果事情是等於18則它不能b e等於19,所以你提供了錯誤的表達。你應該使用「OR」和括號。您的最終表達應如下所示:

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