Adding custom methods to a subclassed NSManagedObject

Posted by CJ on Stack Overflow See other posts from Stack Overflow or by CJ
Published on 2010-01-27T21:24:17Z Indexed on 2010/05/10 15:24 UTC
Read the original article Hit count: 360

Filed under:
|
|
|

I have a Core Data model where I have an entity A, which is an abstract. Entities B, C, and D inherit from entity A. There are several properties defined in entity A which are used by B, C, and D.

I would like to leverage this inheritance in my model code. In addition to properties, I am wondering if I can add methods to entity A, which are implemented in it's sub-entities.

For example:

  1. I add a method to the interface for entity A which returns a value and takes one argument
  2. I add implementations of this method to A, B, C, D
  3. Then, I call executeFetchRequest: to retrieve all instances of B
  4. I call the method on the objects retrieved, which should call the implementation of the method contained in B's implementation

I have tried this, but when calling the method, I receive:

[NSManagedObject methodName:]: unrecognized selector sent to instance

I presume this is because the objects returned by executeFetchRequest: are proxy objects of some sort.

Is there any way to leverage inheritance using subclassed NSManagedObjects?

I would really like to be able to do this, otherwise my model code would be responsible for determining what type of NSManagedObject it's dealing with and perform special logic according to the type, which is undesirable.

Any help is appreciated, thanks in advance.

© Stack Overflow or respective owner

Related posts about core-data

Related posts about subclass