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: Quincey Morris <email@hidden>
- Date: Wed, 16 Sep 2009 15:49:25 -0700
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