2017-03-06 133 views
0

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

谓词就是这样,

[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]]; 

也没有工作。任何想法,我做错了。

+0

入住这http://stackoverflow.com/questions/37919934/nspredicate-for-exact-match – kb920

+0

检查了这一点,但无法得到它如何为“fetchRequest”的工作逻辑 –

+0

@SantanuDasAdhikary检查我回答一次。 –

回答

0

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

谢谢,工作正常:) –

+0

@SantanuDasAdhikary欢迎伴侣:) –