I am trying to write an abstract class to manage managed entities. However, I am not that familiar with Objective-C and wonder how to do the following.
In my abstract class controller I have my ManagedObjectContext.
In my objectController (which implements the abstractController) I need to create an entity.
In the abstract class I have the following:
- (NSManagedObject *) getNewObject {
return (NSManagedObject *)[NSEntityDescription insertNewObjectForEntityForName:_objectName inManagedObjectContext:managedObjectContext];
}
Then in my implementation I call upon this:
MyObject* object = (MyObject) [self getNewObject];
This however gives a compiler error: “Cast to incomplete type”
(MyObject is of type NSManagedObject)
What is the best way. Should I use NSClassFromString? But how do I then define “getNewObject”, something like:
- (id) getNewObject {
return (NSClassFromString (classname)*)[NSEntityDescription insertNewObjectForEntityForName:_objectName inManagedObjectContext:managedObjectContext];
}
Castin is done as below