核心数据谓词"无法解析格式字符串"

时间:2014-05-27 16:06:42

标签: objective-c core-data nspredicate format-string

我正在尝试获取一组与其他对象具有特定关系的NSManagedObjects

所以我有4个托管对象:UserPickGameGroupUser。 UserPick与GameGroupUser分别有一对一的关系。我需要查找具有特定UserPickGameGroup的所有User,但我似乎无法正确构建谓词:

我试过了:

[NSPredicate predicateWithFormat:@"%K = %@, %K = %@, %K = %@",
UserPickRelationships.game, game,
UserPickRelationships.group, group,
UserPickRelationships.user, user]

[NSPredicate predicateWithFormat:@"%K = %@, %K = %@, %K = %@",
UserPickRelationships.game, game.objectID,
UserPickRelationships.group, group.objectID,
UserPickRelationships.user, user.objectID]

最后:

[NSPredicate predicateWithFormat:@"%K.%K = %@, %K.%K = %@, %K.%K = %@",
UserPickRelationships.game, @"objectID", game.objectID,
UserPickRelationships.group, @"objectID", group.objectID,
UserPickRelationships.user, @"objectID", user.objectID]

我尝试在每个可能的组合中使用%@%K。我所得到的只有:NSPredicate cannot parse format string。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:4)

您可能希望将条件与“AND”结合使用:

[NSPredicate predicateWithFormat:@"%K = %@ AND %K = %@ AND %K = %@",
UserPickRelationships.game, game,
UserPickRelationships.group, group,
UserPickRelationships.user, user]

(假设UserPickRelationships.game评估为字符串"game")。