NSManagedObject 的子类化

发布于 2024-11-26 02:05:58 字数 437 浏览 1 评论 0原文

我有一个名为“医院”的核心数据实体。共有三个字段:名称(字符串)、纬度(双精度)和经度(双精度)。 我有一个名为 Hospitals.h/.m 的 NSManagedObject 类,它可以让我很好地获取数据。

我还确定了我当前的 CLLocation。

现在,当我加载所有医院记录时,我想逐步浏览它们并计算我当前距医院的距离。没问题。

啊,但现在我需要将 CLLocationDistance 存储在某个地方。首先,我只是使用 iVars 创建了一个新类来保存 1) HospitalManagedObject 和 2) CLLocationDistance。但这变得非常尴尬。

因此,我想我只需扩展 Hospitals 托管对象类以包含变量“myDistance”。但我尝试的一切都会导致子类管理对象停止与核心数据通信。

最好的方法是什么?

非常感谢任何见解。

I have a core data entity called Hospitals. There are three fields: name(string), latitude(double), and longitude(double).
I have an NSManagedObject class called Hospitals.h/.m that lets me fetch data just fine.

I also have determined my current CLLocation.

Now, when I load all of my hospital records, I would like to step through them and calculate my current distance from the hospital. No problem.

Ah, but now I need to store the CLLocationDistance somewhere. At first, I just created a new class with iVars to hold 1) the HospitalManagedObject and 2) the CLLocationDistance. But that became very awkward.

So, I thought I would just extend the Hospitals managed object class to include a variable "myDistance." But everything I am trying causes the sub-classed managed object to stop wanting to communicate with core data.

How is the best way to do this?

Any insights are very-much appreciated.

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

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

发布评论

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

评论(1

゛时过境迁 2024-12-03 02:05:58

您可以向 Hospital 对象的对象模型添加一个名为 myDistance 的新字段,并使其在数据模型中的类型为 Undefined。这将告诉 Core Data 您有一个将在运行时使用的属性,但它不会存储在数据库中。

在 Hospital.h 中,您可以使用您想要的任何类型定义属性(我假设距离是 NSNumber)

@property (nonatomic, retain) NSNumber *myDistance;

,在您的实现中,您可以将其放入 @dynamic myDistance; 语句中,就像这样所有其他数据字段。

You could add a new field to your object model for Hospital object called something like myDistance and make it of type Undefined in the datamodel. This will tell Core Data the you have a property that you are going to use at runtime but it isn't something that is going to be stored in the database.

In your Hospital.h you define the property using whatever type you want (I am assuming the distance is an NSNumber) like this

@property (nonatomic, retain) NSNumber *myDistance;

and in your implementation you can put it in a @dynamic myDistance; statement just like all of the other data fields.

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