How to check if an objects is inside of a NSSet of objects by using predicateWithFormat in CoreData?

一世执手 提交于 2019-12-06 05:15:19

问题


In my iPhone app, I try to have a TableViewController to display a list of photos those share one same tag (currentTag in code below). Photo and tag are "many to many" relationship in database. Each photo has an attribute named "tags", which type is NSSet. Each tag has an attribute named "photos", which type is NSSet, too. Tag has an attribute called "name".

I'm trying to do the following code:

 NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Photo"];
 request.predicate = [NSPredicate predicateWithFormat:@"tags contains %@",currentTag];

The problem is I can't do many things inside predicateWithFormat for the reason of quotation marks. And the key word "contains" not working here, they are for strings only. I also tried

 [NSPredicate predicateWithFormat:@"%@ IN tags",currentTag]

no luck either...

One more, I found someone has a similar question at here, then I try the following code, still nothing displays in the table view controller. However, if I comments the line, all photos shows up.

 [NSPredicate predicateWithFormat:@"self in %@",[currentTag photos]]

Can someone give help please?


回答1:


Use ANY:

[NSPredicate predicateWithFormat:@"ANY tags == %@",currentTag];


来源:https://stackoverflow.com/questions/10446673/how-to-check-if-an-objects-is-inside-of-a-nsset-of-objects-by-using-predicatewit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!