关于核心数据获取属性的两个基本问题

发布于 2024-11-01 15:13:32 字数 194 浏览 0 评论 0原文

假设我有两个实体:课堂和学生。课堂与学生是一对多的关系。每个学生都有一辆车(hascar 为 1)或没有(hascar 为 0)

我想在课堂上创建几个获取的属性:

  1. 拥有汽车的学生数量
  2. 学生

数量令我困惑的是语法。如何构建一个谓词来检查特定教室中的所有学生?

谢谢你!

Let's say I have two entities: Classroom and Student. The Classroom has a to-many relationship with student. Every student has a car (hascar is 1) or does not (hascar is 0)

I would like to create a couple of fetched properties on Classroom:

  1. number of students that have a car
  2. number of students

What is tripping me up is the syntax. How do I build a predicate that examines all of the students in a particular classroom?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱我好吗 2024-11-08 15:13:32

您不需要获取或获取的属性来检查与特定教室相关的所有学生,因为该关系会自动找到学生。

ClassRoom{
    roomNumber:number
    teacher:string
    students<-->>Student.classroom
}

Student{
    name:string
    classroom<<-->ClassRoom.students.
}

假设您有一个特定的 ClassRoom 对象,aClassRoom。键 aClassRoom.students 返回所有相关 Student 对象的 NSSet。然后你需要做的就是使用集合运算符来获取你想要的信息。

学生的数量很简单:

NSNumber *studentCount=[aClassRoom.students valueForKeyPath:@"@count"];

拥有汽车的学生的数量:

NSPredicate *p=[NSPredicate predicateWithFormat:@"hasCar==1"];
NSNumber *withCars=[[aClassRoom.students filteredSetUsingPredicate:p] valueForKeyPath:@"@count"];

如果您有某种关系,那么您不需要通过获取来查找有关该关系中的对象的信息。

You don't need a fetch or a fetched property to examine all the students related to a particular classroom because the relationship finds the students automatically.

ClassRoom{
    roomNumber:number
    teacher:string
    students<-->>Student.classroom
}

Student{
    name:string
    classroom<<-->ClassRoom.students.
}

Suppose you have a particular ClassRoom object, aClassRoom. The key aClassRoom.students returns a NSSet of all the related Student objects. All you need to do then is to use collection operators to get the information you want.

The number of students would be simple:

NSNumber *studentCount=[aClassRoom.students valueForKeyPath:@"@count"];

The number of students with cars:

NSPredicate *p=[NSPredicate predicateWithFormat:@"hasCar==1"];
NSNumber *withCars=[[aClassRoom.students filteredSetUsingPredicate:p] valueForKeyPath:@"@count"];

If you have a relationship, you don't ever need a fetch to find something about the objects in that relationship.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文