Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
388 views
in Technique[技术] by (71.8m points)

ios - Use NSManagedObject class without initWithEntity:?

My problem is similar to: Problem creating NSManagedObject derived class

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

It's giving me the following error: CoreData: error: Failed to call designated initializer on NSManagedObject class 'Store'

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?

Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...