开发者

Sub-classing an NSManagedObject

开发者 https://www.devze.com 2023-03-22 02:40 出处:网络
I have a core data entity called Hospitals. There are three fields: name(string), latitude(double), and longitude(double).

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 CLLocationDis开发者_Go百科tance 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号