I want to create a button,ie “Done”,when pressed, save all the data into Core Data,I am using the codes below
- (IBAction)done:(id)sender
{
Player *player = [[Player alloc] init];
player.name = self.nameTextField.text;
player.game = game;
player.rating = 1;
[self.delegate playerDetailsViewController:self didAddPlayer:player];
NSManagedObjectContext *context = [self managedObjectContext];
Player *player = [NSEntityDescription
insertNewObjectForEntityForName:@"Player"
inManagedObjectContext:context];
player.name = self.nameTextField.text;
player.game = game;
player.rating = 1;
NSError *error;
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
}
but I keep getting errors like:
Semantic Issue: Use of undeclared identifier ‘NSEntityDescription’; did you mean ‘kSecAttrDescription’?
Receiver type ‘PlayerDetailsViewController’ for instance message does not declare a method with selector ‘managedObjectContext’
Redefinition of ‘player’
Use of undeclared identifier ‘NSEntityDescription’; did you mean ‘kSecAttrDescription’?
Bad receiver type ‘CFTypeRef’ (aka ‘const void *’)
Receiver type ‘NSManagedObjectContext’ for instance message is a forward declaration
Any ideas?
You define Player two times with same variable name:
and
Second one should have different variable name. Also, You should not alloc CoreData object through alloc/init. Also, call didAddPlayer with player created by NSEntityDescription. The best place for it is after save: as only there You are sure that it was properly saved.
If You are not using ARC there is also memory leak, as You do alloc/init and there is no release.