创建 NSManagedObject 的子类 [英] Creating a Subclass of a NSManagedObject
问题描述
我有一个子类 NSManagedObject
类:
@interface MapStateDB : NSManagedObject@结尾
现在我想创建一个 MapStateDB
类的子类:
.h
@interface MapState : MapStateDB@property (weak, nonatomic) id 代表;@结尾
.m
@implementation MapState@synthesize 委托 = _delegate;@结尾
除非我尝试创建一个 MapState
对象,否则它会失败:
MapState *mapState = [MapState MR_createEntityInContext:context];DLog(@"mapState: %@", mapState);
输出:
DEBUG |-[LoginViewController testStuff] |地图状态:(空)
我可以很好地创建一个 `MapStateDB" 对象:
MapStateDB *mapStateDB = [MapStateDB MR_createEntityInContext:context];DLog(@"mapStateDB: %@", mapStateDB);
输出:
DEBUG |-[LoginViewController testStuff] |地图状态数据库:<地图状态数据库:0x7d432d00>(实体:MapStateDB;id:0x7d424640
最初的意图是为 NSManagedObject
提供一个委托,并在另一个问题中建议将我的 NSManagedObject
子类化:
我使用 Xcode 创建子类 NSManagedObject
并创建了 FooMapState.h/.m 文件(而不仅仅是 MapState.h/.m 文件):
@interface FooMapState : NSManagedObject@结尾
FooMapState 和 MapState 是相同的,只是名称不同.它们都是 NSManagedObject
的子类,这使得创建委托变得困难(回到我发布的原始问题).
您遇到问题,因为您正在尝试创建实体 MapState
的实例,但在您的数据模型——实体名称是 MapStateDB
.
拥有 MapState
子类是可以的,但是为了创建实例工作,您需要为数据模型中的实体设置正确的类名.因此,在 MapStateDB
实体上,将类名设置为 MapState
(实际上,您可以在其中键入任何类名).然后,当您创建 MapStateDB
实体的新实例时,您将获得 MapState
类的实例.
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).
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.
这篇关于创建 NSManagedObject 的子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!