NSManagedObject 的子类化
我有一个名为“医院”的核心数据实体。共有三个字段:名称(字符串)、纬度(双精度)和经度(双精度)。 我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以向 Hospital 对象的对象模型添加一个名为
myDistance
的新字段,并使其在数据模型中的类型为Undefined
。这将告诉 Core Data 您有一个将在运行时使用的属性,但它不会存储在数据库中。在 Hospital.h 中,您可以使用您想要的任何类型定义属性(我假设距离是 NSNumber)
,在您的实现中,您可以将其放入
@dynamic myDistance;
语句中,就像这样所有其他数据字段。You could add a new field to your object model for Hospital object called something like
myDistance
and make it of typeUndefined
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
and in your implementation you can put it in a
@dynamic myDistance;
statement just like all of the other data fields.