objective-c - 核心数据谓词对多

标签 objective-c ios core-data nspredicate

我正在尝试找到匹配字符串和一组对象的对象。我的谓词看起来像这样:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@ and individuals CONTAINS %@", name, individuals];

我没有任何点击。虽然我知道有一个匹配名称和个人设置的实体。

我的谓词有什么问题?

编辑:我已经设法取得一些进展。现在的问题是,如果我尝试找到一个具有现有组名和现有联系人的组,即groupname =“test”和personal.name =“john doe”和personal.contactInfo =“123”,它将正确地找到该组但是,如果我有一个具有相同组名,相同联系人+另一个联系人的组,它也会找到我不想要的该组。

我只想要与谓词完全匹配的组。

我现在通过这样做来使用子谓词:
NSMutableArray *subPredicates = [NSMutableArray initWithCapacity:5];
// Add group name to predicate
[subPredicates addObject:[NSPredicate predicateWithFormat:@"name == %@", name]];

for (NSDictionary *contactInfo in individuals) {

    NSString *name = [contactDict objectForKey:@"name"];
    NSString *contactInfo = [contactDict objectForKey:@"contactInfo"];

    NSPredicate *individualPredicate = [NSPredicate predicateWithFormat:@"ANY individuals.name LIKE %@ AND any individuals.contactInfo LIKE %@", name, contactInfo];

    [subPredicates addObject:individualPredicate];
}

NSPredicate *individualsPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:subPredicates];

Group *group = // do the fetch with the predicate

最佳答案

我在电话上输入此内容,无法验证,但以下谓词可能对您有用:

[NSPredicate predicateWithFormat:@"name == %@ AND SUBQUERY(individuals, $x, $x IN %@).@count == %d", 
    name, individuals, individuals.count];

它搜索具有给定名称的对象,这些对象的个体是给定集合的超集。

关于objective-c - 核心数据谓词对多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13124738/

相关文章:

iphone - 核心数据获取结果 Controller 和自定义节标题

ios - 核心数据 : Quickest way to delete all instances of an entity

iphone - Objective-C 矩形 CGPoint 到极坐标 CGPoint

ios - iOS UIBarButton和应用拒绝

iphone - 将字典加载到应用程序中的最佳方式? iOS

iphone - 我想在加载新图像时删除 tableviewcell 中的图像。在 iOS 中

ios - 核心数据 : 'The left hand side for an ALL or ANY operator must be either an NSArray or an NSSet.'

ios - 在 viewWillAppear 中调用 performFetch 不起作用

ios - 加载所有内容后如何更改uitableview单元格的字体大小

android - GoogleAppIndex 真的为应用程序的内容编制索引吗?