在coreData xcode iphone中创建复合谓词 [英] create a Compound Predicate in coreData xcode iphone

查看:157
本文介绍了在coreData xcode iphone中创建复合谓词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理有3个实体(班级,学生,考试记录)的核心数据及其关系区域:

HI i am working on the core data with 3 entities (Class,Students,ExamRecord) and their relations area as :

Class<------>> Students <------> ExamRecord

我创建了一个用于获取第5级学生列表的谓词。

I created a predicate for fetching list of students for class 5th.

NSString * fmt2 = @"studentsToClass.className=%@";
NSPredicate * p2 = [NSPredicate predicateWithFormat:fmt2,@"5th",nil];

这样我得到第五级的所有学生

with this i am getting all students of class 5th

现在我还想对学生提交另一个过滤器。

Now i also want to apply another filter on the Students fetched.

获取考试记录结果为通过的学生.result是ExamResult实体中学生的属性

Fetch students whose Exam Record "result" is "Pass".result is an attribute for student in ExamResult entity

如何在这里使用复合谓词?

How can i make use of Compound predicate in this ?

如果我错了,请更正我。

Please correct me if i am wrong

感谢

推荐答案

复合谓词:

NSPredicate *p1 = [NSPredicate predicateWithFormat:@"studentsToClass.className = %@", @"5th"];
NSPredicate *p2 = [NSPredicate predicateWithFormat:@"studentsToExamRecord.result = %@", @"Pass"];
NSPredicate *p = [NSCompoundPredicate andPredicateWithSubpredicates: @[p1, p2]];

或者您只需将测试与AND结合:

Or you simply combine the tests with "AND":

NSPredicate *p = [NSPredicate predicateWithFormat:@"studentsToClass.className = %@ AND studentsToExamRecord.result = %@",
      @"5th", @"Pass"];

请注意, predicateWithFormat 的参数列表不 nil - 已终止。
参数的数量由格式为
string的格式说明符的数量决定。

Note that the argument list of predicateWithFormat is not nil-terminated. The number of arguments is determined by the number of format specifiers in the format string.

这篇关于在coreData xcode iphone中创建复合谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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