如何通过在CoreData中使用predicateWithFormat来检查对象是否位于NSSet对象中? [英] How to check if an objects is inside of a NSSet of objects by using predicateWithFormat in CoreData?

查看:183
本文介绍了如何通过在CoreData中使用predicateWithFormat来检查对象是否位于NSSet对象中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iPhone应用程序,我尝试有一个TableViewController显示共享同一个标签的照片的列表(currentTag在下面的代码)。照片和标签在数据库中是多对多的关系。每张照片都有一个名为tags的属性,其类型为NSSet。每个标签都有一个名为photos的属性,其类型也是NSSet。标记具有名为name的属性。



执行以下代码:

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

问题是我不能在predicateWithFormat里面做很多事情,原因是引号。而关键词contains在这里不工作,他们只是为字符串。我也尝试了

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



没有运气...



问题在这里,然后我尝试以下代码,仍然没有显示在表视图控制器。

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

有人可以帮助吗?

解决方案

使用ANY:

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


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?

解决方案

Use ANY:

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

这篇关于如何通过在CoreData中使用predicateWithFormat来检查对象是否位于NSSet对象中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆