我正在尝试获取一组与其他对象具有特定关系的NSManagedObjects
。
所以我有4个托管对象:UserPick
,Game
,Group
,User
。 UserPick与Game
,Group
,User
分别有一对一的关系。我需要查找具有特定UserPick
,Game
和Group
的所有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
。我怎样才能做到这一点?
答案 0 :(得分:4)
您可能希望将条件与“AND”结合使用:
[NSPredicate predicateWithFormat:@"%K = %@ AND %K = %@ AND %K = %@",
UserPickRelationships.game, game,
UserPickRelationships.group, group,
UserPickRelationships.user, user]
(假设UserPickRelationships.game
评估为字符串"game"
)。