Say I have a subclass of NSManagedObject
called MBManagedSquare
and MBManagedCircle
. An MBManagedSquare
and MBManagedCircle
define a method prepareFromDictionary:(NSDictionary*)dic
, and both of their implementations are different.
Say I have this code:
NSString *type = // could be @"MBManagedSquare" or @"MBManagedCircle"
NSEntityDescription *desc = [NSEntityDescription entityForName:type inManagedObjectContext:_context];
NSManagedObject *object = [[NSManagedObject alloc] initWithEntity:desc insertIntoManagedObjectContext:_context];
So the type of entity it will be with Core Data is determined dynamically via a type
string. So all I know is that it is an NSManagedObject
.
What I want to do is call the prepareFromDictionary:
method for the right class.
So if the type is "MBManagedSquare", I want to cast the object to an MBManagedSquare
, and then call
[castedObject prepareFromDictionary:dic];
What I tried doing is:
Class class = NSClassFromString(type);
class *castedObject = (class*)object;
but I get an expected expression error. I'm not sure if this is even possible. How would I do this?