Creating a Subclass of a NSManagedObject

梦想的初衷 提交于 2020-01-16 05:36:06

问题


I have a subclassed NSManagedObject class:

@interface MapStateDB : NSManagedObject

@end

Now I want to make a subclass of MapStateDB class:

.h

@interface MapState : MapStateDB

@property (weak, nonatomic) id <MapStateDelegate> delegate;

@end

.m

@implementation MapState

@synthesize delegate = _delegate;

@end

Except when I try to create a MapState object, it fails:

MapState *mapState = [MapState MR_createEntityInContext:context];

DLog(@"mapState: %@", mapState);

output:

DEBUG | -[LoginViewController testStuff] | mapState: (null)

I can create a `MapStateDB" object just fine:

MapStateDB *mapStateDB = [MapStateDB MR_createEntityInContext:context];
DLog(@"mapStateDB: %@", mapStateDB);

output:

DEBUG | -[LoginViewController testStuff] | mapStateDB: <MapStateDB: 0x7d432d00> (entity: MapStateDB; id: 0x7d424640 <x-coredata:///MapStateDB/tF842754F-4044-44E5-A9D5-118389ABF4AA2>

The original intent is to provide a delegate to a NSManagedObject and was suggested in another question to subclass my NSManagedObject: NSManagedObject with Category and Delegate

Any ideas why the subclassed MapState object isn't being created?

EDIT:

I replaced the Magical Record method [MR_createEntityInContext:] with the Objective-C standard [insertNewObjectForEntityForName:inManagedObjectContext:]:

MapState *mapState2 = [NSEntityDescription insertNewObjectForEntityForName:@"MapState" inManagedObjectContext:context];
DLog(@"mapState2: %@", mapState2);

output:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an entity named 'MapState' in this model.'

So Magical Record was catching the error and returning nil. So I guess @Rickster's question is pointing to the issue, my Core Data model has no idea what MapState is.

Could I make a transient property for the delegate? OR should I just use a wrapper class?

Is it possible to get a NSManagedObject with a delegate property?

EDIT 2:

I tried changing the MapStateDB entities' class to FooMapState:

I used Xcode to create the subclassed NSManagedObject and it created FooMapState.h/.m files (instead of just MapState.h/.m files):

@interface FooMapState : NSManagedObject

@end

The FooMapState and MapState are identical, just with different names. They are both subclassed NSManagedObject which makes creating a delegate difficult (back to the original question I posted).


回答1:


You're having problems because you're trying to create an instance of the entity MapState, but there is no such entity in your data model-- the entity name is MapStateDB.

It's OK to have the MapState subclass, but in order for creating instances to work, you need to set the right class name for the entity in your data model. So on the MapStateDB entity, set the class name to MapState (You can type any class name there, really). Then when you create a new instance of the MapStateDB entity, you'll get an instance of the MapState class.



来源:https://stackoverflow.com/questions/34049208/creating-a-subclass-of-a-nsmanagedobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!