0

I have created the following example Core Data NSManagedObject subclasses:

PBCommentableObject : NSManagedObject // to allow comments on object
@property (nonatomic, retain) NSSet *pBcomments; // unordered set of PBComment objects

PBComment : PBCommentableObject // to allow comments on a comment
@property (nonatomic, retain) PBCommentableObject *target;

PBList : PBCommentableObject // to allow comments on a list
@property (nonatomic, retain) NSOrderedSet *pBorderedItems; // ordered set of PBListableObject objects

PBString : PBListableObject // to allow strings to be added to lists
@property (nonatomic, retain) NSString *pBtext;

PBListableObject : ???? // I'd like both PBList and PBString to be PBListableObjects
@property (nonatomic, retain) PBList *pBlist;

The problem I am having is that I would like the following behavior:

  • Lists (PBList) that can hold an ordered list of strings (PBString) or other lists (PBList).
  • Allow comments (PBComment) on lists (PBList) and other comments (PBComment) but not strings (PBString)

Is this possible to do? I am currently trying to build the Core Data model via the visual interface in Xcode; and while I could conceive of using categories, I don't know how I would do so without Xcode spitting out a warning when I fail to define a relationship via the visual editor.

Ken M. Haggerty
  • 24,902
  • 5
  • 28
  • 37
  • As everywhere in objective-C you cannot inherit from more than one parent. You could of course introduce independent entities to which you refer from more than one entity. But that is not exactly what you are up to. – Hermann Klecker Jul 23 '13 at 20:19

1 Answers1

0

I think you are stretching the idiom of inheritance. All this "listable" and "commentable" results in quite unreadable complexity.

The validation of allowing or disallowing comments, lists or what have you, can be modeled with simply using (boolean) attributes and relationships. Try to think of only concrete objects such as "Comment" or "String" and model the lists with the attribute sets.

Mundi
  • 79,884
  • 17
  • 117
  • 140