使用NSManagedObject类,而不使用initWithEntity :? [英] Use NSManagedObject class without initWithEntity:?

查看:128
本文介绍了使用NSManagedObject类,而不使用initWithEntity :?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题类似于:创建NSManagedObject派生类问题

我在Core Data中设置了一个NSManagedObject,并且有一个类。但是,我不想创建一个相同的NSObject类,我想使用NSManagedObject类,但我不想创建实体并保存它。我只想使用它为一个数组,只有当我需要保存对象在Core Data中,我想使用 insertEntity:

I have setup a NSManagedObject in Core Data and have a class for it. However, instead of creating an identical NSObject class, I'd like to use the NSManagedObject class, but I don't want to create the entity and save it. I just want to use it for an array, only when I need to save the object in Core Data do I want to use insertEntity:

Store * store = [[Store alloc] init];

Store *store = [[Store alloc] init];

这会给我以下错误: CoreData:error:无法调用NSManagedObject类'Store'上的指定初始化程序

是否有一种方法可以使用NSManagedObject类/

Is there a way to either subclass or somehow use the NSManagedObject class/properties to allocate objects I am just using temporarily for a table?

推荐答案

只需使用 initWithEntity:insertIntoManagedObjectContext:并传递 nil 上下文,然后在 NSMAnagedObjectContext 中调用 insertObject:

Just use initWithEntity:insertIntoManagedObjectContext: and pass a nil context, then call insertObject: in your NSMAnagedObjectContext when you are ready:

NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyModelClass" inManagedObjectContext:myContext];
id object = [[MyModelClass alloc] initWithEntity:entity insertIntoManagedObjectContext:nil];

这篇关于使用NSManagedObject类,而不使用initWithEntity :?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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