NSPredicate 搜索逗号分隔值与数组
NSPredicate search comma separated value with array
有一个名为 "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]];
也不行。知道我哪里做错了。
您需要从 predicate
中删除 keywords contains[cd] %@
,然后 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]];
有一个名为 "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]];
也不行。知道我哪里做错了。
您需要从 predicate
中删除 keywords contains[cd] %@
,然后 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]];