Core Data : "insertNewObjectForEntityForName" question
I'm trying开发者_高级运维 to use "insertNewObjectForEntityForName", but I've got a problem with my declaration, I wondered if someone had an opinion on this .
This is my implementation :
NSManagedObjectContext *context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
// Create Object
Shots *newShot = (Shots *)[NSEntityDescription insertNewObjectForEntityForName:@"Shots" inManagedObjectContext:context];
It's complaining that "Shots" is undeclared, now my question is : Do I need to declare a new class for Shots ? Knowing that it appears as a NSManagedObject in the dataModeler ?
No, you do not need to write your own "Shots" class. In which case, use "NSManagedObject" as the data type instead. It'll respond to accessing the Shots data fields.
NSManagedObject *newShot = [NSEntityDescription insertNewObjectForEntityForName:@"Shots" inManagedObjectContext:context];
You can write a Shots class, it will have NSManagedObject as its base class, if you need to implement model behavior particular to the Shots objects.
Have you included #import "Shots.h
" at the top of this file?
I am not sure what you mean by "undeclared", but an NSManagedObject (Core Data Entity) is just like any other object; if you want to use it, you need to import it.
you will also probably need to click on your shots entity in the datamodule and then click file->new and you will be presented with a new item called "managed Object class" once you u go through the steps it will write down that class as you made the entity in the dataModule.
精彩评论