【问题标题】:NSPredicate search comma separated value with arrayNSPredicate 用数组搜索逗号分隔值
【发布时间】:2017-03-06 07:08:52
【问题描述】:

有一个名为 "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]];

也不行。知道我哪里做错了。

【问题讨论】:

标签: ios objective-c arrays nspredicate nscompoundpredicate


【解决方案1】:

您需要从您的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]];

【讨论】:

  • @SantanuDasAdhikary 欢迎朋友 :)
猜你喜欢
  • 2018-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-14
  • 1970-01-01
相关资源
最近更新 更多