NSPredicate 按属性过滤核心数据关系 NSSet
NSPredicate to filter Core Data Relationship NSSet by attribute
我与两个实体有 many-to-many
关系。一个是Person
,另一个是Clubs
。我希望能够编写一个 predicate
来查找特定 Club
中的所有 Person
个实体。我也想检查那个人的 position
属性。
这是不起作用的:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.managedObjectContext]];
[request setPredicate:[NSPredicate predicateWithFormat:@"position CONTAINS[cd] %@ AND IN %@", @"manager", self.clubs.people]];
NSArray *results = [self.managedObjectContext executeFetchRequest:request error:nil];
这会因错误而崩溃:
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: 'Unable to parse the format
string "title CONTAINS[cd] %@ AND IN %@"'
我知道我做错了什么,有什么帮助吗?
正如上面@pbasdf 所指出的,您应该使用 self.但是,首先使用 self 子句的查询会更有效率,即
[NSPredicate predicateWithFormat:@"SELF IN %@ AND position CONTAINS[cd] %@", self.clubs.people, @"manager"];
我与两个实体有 many-to-many
关系。一个是Person
,另一个是Clubs
。我希望能够编写一个 predicate
来查找特定 Club
中的所有 Person
个实体。我也想检查那个人的 position
属性。
这是不起作用的:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.managedObjectContext]];
[request setPredicate:[NSPredicate predicateWithFormat:@"position CONTAINS[cd] %@ AND IN %@", @"manager", self.clubs.people]];
NSArray *results = [self.managedObjectContext executeFetchRequest:request error:nil];
这会因错误而崩溃:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "title CONTAINS[cd] %@ AND IN %@"'
我知道我做错了什么,有什么帮助吗?
正如上面@pbasdf 所指出的,您应该使用 self.但是,首先使用 self 子句的查询会更有效率,即
[NSPredicate predicateWithFormat:@"SELF IN %@ AND position CONTAINS[cd] %@", self.clubs.people, @"manager"];