0

有一个名为的实体"keyword"字段,其中包含逗号分隔值,例如"8275,8276,8277". 现在使用 NSPredicate 搜索关键字匹配 this 并且传递值是 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]];

也不工作。知道我在哪里做错了。

4

1 回答 1

1

您需要keywords contains[cd] %@从您的predicatethenCompoundPredicate作品中删除。

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]];
于 2017-03-06T07:16:21.950 回答