I have a category for NSManagedObjectContext , which inserts an NSManagedObject in another NSManagedObjectContext and returns item:
@implementation NSManagedObjectContext (GTEntity) - (id)addEntity:(id)entity { NSManagedObject *entityObject = [self objectWithID:[entity objectID]]; return entityObject; } @end My question is how I return object the same type as I've got. I mean, if I call [ctx addEntity:city] with City *city, where the City is a NSManagedObject subclass, I would like to get the object in other context but the same type, not in NSManagedObject type. This type is generic and should be determinated at the runtime from entity, like objc_getClass(entity). I found some macros, but it has some strange errors Expected "]" after (cls *):
#define objc_dynamic_cast(obj, cls) \ ([obj isKindOfClass:(Class)objc_getClass(#cls)] ? (cls *)obj : NULL)