• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to create subentity object inheriting from superentity object in core data
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >Re: How to create subentity object inheriting from superentity object in core data (From: Leon Starr <email@hidden>)

  • Prev by Date: Re: NSUserDefaults synchronization in Foundation tools
  • Next by Date: Re: NSToolTipOwner Protocol and Xcode 3.2 build failure in Debug Build Configuration
  • Previous by thread: Re: How to create subentity object inheriting from superentity object in core data
  • Next by thread: Re: How to create subentity object inheriting from superentity object in core data
  • Index(es):
    • Date
    • Thread