0
votes

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?

2
You've got a variety of different errors stemming from a variety of different problems in your code. You should start from the top of the list and deal with each one individually. SO is a great resource for figuring out error messages: just search on the first part of each message. For example, searching for [xcode] Redefinition turns up this question which will probably help you. But please don't ask us to fix all the errors in your code just because you didn't bother to look them up yourself.Caleb

2 Answers

2
votes

Make sure you have

#import <CoreData/CoreData.h>

somewhere in your headers, either directly in your file or in your app's pch file. It should be there by default when you create a Core Data project but if you added Core Data later, it might be missing. Here's what a pch looks like for a Core Data project:

#import <Availability.h>

#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif

#ifdef __OBJC__
  #import <UIKit/UIKit.h>
  #import <Foundation/Foundation.h>
  #import <CoreData/CoreData.h>
#endif
0
votes

You define Player two times with same variable name:

Player *player = [[Player alloc] init];

and

Player *player = [NSEntityDescription insertNewObjectForEntityForName:@"Player" inManagedObjectContext:context];

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.