Can CoreData handle polymorphic subclasses?

Viewed 5073

I have a CoreData model (managed object) called Item:

@interface Item :  NSManagedObject {
  NSString * type;
}
@property (retain) NSString * type;
@end

I also have two subclasses of Item:

@interface Circle : Item
@end

@interface Square : Item
@end

I track the subclass of the item by the type property.

When I fetch my Items, I get back an array of Items. But I want to be able to dynamically typecast the items according to their type.

Does CoreData support this natively? If not, is there a way to dynamically typecase each Item?

I can get the class that I want to cast the Item as like:

Item * item = ...;
id klass = NSClassFromString(item.type);

I just don't know how I can cast item as type klass.

1 Answers
Related