CoreData - 如何创建NSManagedObject子类的子类(Generation Gap pattern) [英] CoreData - how to create a subclass of a subclass of NSManagedObject (Generation Gap pattern)

查看:200
本文介绍了CoreData - 如何创建NSManagedObject子类的子类(Generation Gap pattern)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信这肯定是一个愚蠢的问题,但我已经淘汰了网络,找不到答案。许多人谈论使用生成间隙模式,其中有一个NSManagedObject子类从您的模型生成,然后子类添加瞬态属性和行为。这样做的好处是,如果你改变你的持久模型,你可以再次生成你的基本文件,没有任何危险,覆盖你自己的代码。



我有一个基础CardMO对象派生自NSManagedObject。



我的问题是,如何创建一个被管理的Card对象?



我试过:

  Card * card = [NSEntityDescription insertNewObjectForEntityForName:@CardMOinManagedObjectContext:moc]; 

但是这个对象不是一个卡,当然,



我尝试用alloc / init创建一个卡(其中init只是调用了[super init]),然后将它添加到managedObjectContext如下:

  [moc insertObject:(CardMO *)card]; 

这给我一个神秘的错误:无法调用NSManagedObject类'Card' p>

我试图通过调用[NSEntityDescription insertNewObjectForEntityForName:@CardMOinManagedObjectContext:[AIStoreManager sharedAIStoreManager] .managedObjectContext]而不是[super init]。在这种情况下,我回来的对象再次是一个CardMO,我不能调用卡方法。



我应该怎么办?



和(为奖励点:-) - 在我通过这个障碍后,我需要从XML创建我的其他卡对象,然后将它们转换为NSManagedObjects。我使用基于苹果的XMLReaderSAX的代码 - 我把这段代码一大堆XML,它给了我一个很多Card对象的数组。我可以以后将这些对象添加到我的托管对象上下文,或者我必须进入XMLReaderSAX并改变它如何创建这些对象?



我在写一个iPhone应用程序on 3.0,但我认为这对于Core Data for 10.5是一样的。

解决方案


$ b

在数据模型中,实体的类名必须是最大派生类 - 我的case中的卡。麻烦的是,你必须记住在生成任何新文件之前将名称更改为CardMO。



这必须是为什么人们使用 mogenerator


I'm sure this must be a stupid question, but I've scoured the interwebbings and can't find the answer. Plenty of people talk about using the Generation Gap pattern, in which you have an NSManagedObject subclass that is generated from your model, and then subclass that to add transient properties and behaviours. The benefit of this is that if you change your persisted model, you can just generate your base files again without any danger of overwriting your own code.

I have a base CardMO object derived from NSManagedObject. I then subclass this to make my own Card object.

My question is, how do I create a Card object that is managed?

I tried:

  Card* card = [NSEntityDescription insertNewObjectForEntityForName:@"CardMO" inManagedObjectContext:moc];

But this object isn't really a Card, and of course there's an exception when I go on to call a Card method on this object.

I've tried creating a Card with alloc / init (where init just calls [super init]), and then adding it to the managedObjectContext like this:

[moc insertObject:(CardMO*)card];

This gives me the cryptic error "Failed to call designated initializer on NSManagedObject class 'Card'"

I've tried modifying this by calling [NSEntityDescription insertNewObjectForEntityForName:@"CardMO" inManagedObjectContext:[AIStoreManager sharedAIStoreManager].managedObjectContext] instead of [super init]. In this case the object I get back is again a CardMO, and I can't call Card methods on it.

What should I be doing?

And (for bonus points :-) - after I've passed this hurdle, I need to create my other Card objects from XML, and then turn them into NSManagedObjects. I'm using code based on Apple's XMLReaderSAX - I hand this code a chunk of XML, and it gives me back an array of many Card objects. Can I later add these objects to my managed object context, or do I have to get into XMLReaderSAX and change how it creates those objects?

I'm writing an iPhone app on 3.0, but I assume this is the same for Core Data for 10.5.

解决方案

Sussed it!

In the data model, the class name for the entity has to be that of the most derived class - Card in my case. The trouble then is that you have to remember to change the name to CardMO before generating any new files.

This must be why people use mogenerator.

这篇关于CoreData - 如何创建NSManagedObject子类的子类(Generation Gap pattern)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆