NSPredicate使用数组搜索逗号分隔值

时间:2017-03-06 07:08:52

标签: ios objective-c arrays nspredicate nscompoundpredicate

有一个名为"keyword"的实体,其中包含逗号分隔值"8275,8276,8277"。现在使用NSPredicate搜索那些关键字匹配的用户和传递值是NSArray。尝试使用工作单值的(keywords contains[cd] %@)来获取,而不是为数组工作。

谓词就是这样,

[NSPredicate predicateWithFormat:@"((eventId == %@) AND (keywords contains[cd] %@) AND (attendeeIsVisible == %@))",self.appDelegate.currentEvent.entityId,selectedTagID,[NSNumber numberWithInt:1]]

打印谓词后,它就像 -

eventId == 18230 AND keywords CONTAINS[cd] {"8275", "8276", "8277"} AND attendeeIsVisible == 1

尝试复合谓词也喜欢

NSMutableArray *parr = [NSMutableArray array];
for (id locaArrayObject in selectedTagID) {
   [parr addObject:[NSPredicate predicateWithFormat:@"keywords contains[cd] %@ ",locaArrayObject]];
}

predicate = [NSPredicate predicateWithFormat:@"((eventId == %@) AND (keywords contains[cd] %@) AND (attendeeIsVisible == %@))",self.appDelegate.currentEvent.entityId,selectedTagID,[NSNumber numberWithInt:1]];

NSPredicate *predicateObj = [NSCompoundPredicate andPredicateWithSubpredicates:@[predicate, parr]];

也不行。不知道我做错了什么。

1 个答案:

答案 0 :(得分:0)

您需要从keywords contains[cd] %@移除predicate,然后CompoundPredicate为您效果。

NSMutableArray *parr = [NSMutableArray array];
for (id locaArrayObject in selectedTagID) {
    [parr addObject:[NSPredicate predicateWithFormat:@"keywords contains[cd] %@ ",locaArrayObject]];
}

NSPredicate *eventPredicate = [NSPredicate predicateWithFormat:@"((eventId == %@) AND (attendeeIsVisible == %@))",self.appDelegate.currentEvent.entityId,[NSNumber numberWithInt:1]];

NSPredicate *keywordPredicate = [NSCompoundPredicate orPredicateWithSubpredicates: parr];

//Now use below predicate with your array
predicate = [NSCompoundPredicate orPredicateWithSubpredicates: [eventPredicate, keywordPredicate]];