假设我在feedDays数组中有一个谓词数组:
<__NSArrayM 0x7ff7f3cd1040>(
feedDay == 1,
feedDay == 2,
feedDay == 4
)我需要和它一起使用这个statusPredicates数组:
<__NSArrayM 0x7ff7f3cd1070>(
is_neutered == 0 AND other_status == 0,
is_neutered == 0 AND other_status == 1,
is_neutered == 0 AND other_status == 2,
is_neutered == 0 AND other_status == 3,
)我如何将这些合并成一个查询?我想我必须使用NSCompoundPredicate,但是我想不出如何将它和两个谓词数组放在一起。
发布于 2015-09-21 18:52:30
在我看来,一个组合谓词,如
feedDay == 1 && feedDay == 2 ...总是假的。
因此,您可能的意思是将每个谓词与neutered条件组合,以便和条件都为真(and),并将这些单独的组组合起来,从而使--这两种条件中的任何一种--都是真(or)。
如果将数组与and组合成一个大的复合,那么它肯定是假的。假设neutered条件总是相同的,
NSPredicate *finalPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:
@[[NSPredicate predicateWithFormat:@"is_neutered = 0"],
[NSCompoundPredicate orPredicateWithSubPredicates:feedDays]]];很难看,对吧?因此,我建议您使用feedDay的可能值创建一个数组,并使用简单的
[NSPredicate predicateWithFormat:
@"is_neutered = 0 && feedDay in %@", allowedFeedDays]发布于 2015-09-21 18:05:24
用这个代码-
NSMutableArray *allPredicates = [[NSMutableArray alloc] initWithArray:feedDays];
[allPredicates addObjectsFromArray:statusPredicates];
NSCompoundPredicate *finalPred = [NSCompoundPredicate andPredicateWithSubpredicates:allPredicates];https://stackoverflow.com/questions/32701532
复制相似问题