使用没有 initWithEntity: 的 NSManagedObject 类? [英] Use NSManagedObject class without initWithEntity:?

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

问题描述

我的问题类似于:创建 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: Failed to call specified initializer on NSManagedObject class '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 上下文,然后调用 insertObject: 在您准备好后在 NSManagedObjectContext 中:

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];

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

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