Re: How to create subentity object inheriting from superentity object in core data
Re: How to create subentity object inheriting from superentity object in core data
- Subject: Re: How to create subentity object inheriting from superentity object in core data
- From: Leon Starr <email@hidden>
- Date: Wed, 16 Sep 2009 18:38:02 -0700
Okay, my understanding, then, is that the inheritance is just in the
model - makes sense. If you subclass NSManagedObjects for the parent
and child entities, you need to explicitly declare and synthesize
(dynamically) all common properties you want to access at lower levels
of the hierarchy (at least).
Just to be clear, in the model I've got Entities: Auto, Sedan, Truck
and the parent (Auto) has a common property license. Now when I get a
pointer to a Sedan or Truck I want the ability to access via
thisTruck.license or thisSedan.license. In fact, if I end up with a
pointer (Auto *) thisAuto, I want thisAuto.license as well.
To get this to work, without KVC, I need to declare @property/
@dynamic for license in Auto, Sedan and Truck. (Can't just put it in
Auto and expect Sedan.license to work). No big deal unless I have a
bunch of common properties in the parent entity.
Right? (Sorry - no question, just looking for correction if I'm wrong!)
All that said, I can see that I would be better off using KVC and not
subclassing at all unless I really need to for some reason. Thanks
again for the help guys!
- Leon
FROM : Quincey Morris
DATE : Thu Sep 17 00:49:25 2009
On Sep 16, 2009, at 14:55, Leon Starr wrote:
> Okay, guys, this helps a lot, still stuck, but getting closer! Now
> I am getting an error with my revised code. We are talking about
> two entities A and B where B where A is the parent of B.
> A has the property "name", and I would like to set that after
> creating an object for B.
>
> Here is the code:
>
> - (void)create_B: (NSString *)bName{
> NSEntityDescription *bEntity = [[model entitiesByName]
> objectForKey:@"B"];
>
> B *newB = [[OffDutyATC alloc] initWithEntity:bEntity
> insertIntoManagedObjectContext:context];
> newB.name = bName; // << ERROR request for member 'name' in
> something not a structure or union
> }
>
> Now I have defined an NSManagedObject class for both A and B with
> the "name" property/dynamic set for A.name. But the B
> NSManagedObject class does not have any properties defined, so the
> error makes sense, but so much for normal inheritance!
>
> So what is the correct approach here? Should I just use KVC to set
> the name property and not subclass NSManagedObject? Or should I add
> property/dynamic statements to my B subentity to mirror A? That
> seems kind of funky... Basically, what is the correct approach for
> setting the properties of a parent entity?
This part of your code looks fine. You're may have to show us your
@interface declarations for A and B (or at least the relevant parts).
The situation with properties is similar to the situation with
classes. Defining properties (attributes) in your managed object model
is *not* mirrored in your custom subclass(es) unless you do it yourself.
For example, if your model gives entity A a string attribute "name",
your custom A subclass also needs a @property NSString* name, if you
want the convenience of using the a.name syntax. (Or, yes, you can
reference the property via KVC.)
In the simplest case, Core Data provides you with a suitable
implementation of the @property accessors (-name and -setName), so all
you need is, in your @interface:
@property NSString* name;
and in your @implementation:
@dynamic name;
which means "use Core Data's handy-dandy implementation of the
accessors for 'name'".
If B is a subclass of A (by which I mean if entity B's custom subclass
is a subclass of entity A's custom subclass of NSManagedObject), then
B will inherit the "name" @property from A with no additional coding.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden