如何在核心数据中获取关系的关系? [英] How do I fetch a relationship of a relationship in core data?

查看:31
本文介绍了如何在核心数据中获取关系的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 个实体:

建筑物、楼层和房间

关系如下:

建筑 -> 楼层 -> 房间

Building -> Floors -> Rooms

Building -> Floors 是一对多的关系楼层 -> 房间是一对多的关系

Building -> Floors is a To-Many relationship Floors -> Rooms is a To-Many Relationship

我将如何获取房间内的数据?

How would I go about fetching the data within Rooms?

我知道如何使用下面的谓词代码获取 Building -> Floors 但我如何更进一步获取 Room 的数据?

I know how to fetch Building -> Floors with the predicate code below but how do I go a step further and fetch the data of Rooms?

让 fetchFloorPredicate = NSPredicate(格式:"buildingOfFloor.buildingName == %@", buildingName)

let fetchFloorPredicate = NSPredicate(format: "buildingOfFloor.buildingName == %@", buildingName)

推荐答案

您不使用谓词来获取关系,关系也是实体中与您在 Xcode 中命名关系同名的属性.

You don't use a predicate to fetch relationships, a relationship is also a property in your entity with the same name as you named the relationship in Xcode.

因此,如果您有一个 Building 对象 building 并且与 Floors 的关系命名为 floors,则您可以使用

So if you have a Building object building and the relationship to Floors is named floors you get all Floors object that belong to a building using

let floors = building.floors

或者如果你想要它作为一个数组而不是一个集合

or if you want it as an Array instead of a Set

let floors = Array(building.floors)

如果我们假设关系的名称是房间,那么房间也是如此

and the same goes for Rooms if we assume the name of the relationship is rooms

let rooms = floor.rooms

这篇关于如何在核心数据中获取关系的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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