Kaz
Kaz

Reputation: 1466

error when using core data

hello i'm trying to use core data to read and write user data. the code i'm using is as follows:

AppDelegate interface

@interface PopAdsAppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>
{
@private
    NSManagedObjectContext *managedObjectContext_;
    NSManagedObjectModel *managedObjectModel_;
    NSPersistentStoreCoordinator *persistentStoreCoordinator_;
}

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) UITabBarController *tabBarController;

@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;

@end

the code i'm getting error in is this in the .m file:

PopAdsAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"User" inManagedObjectContext:context];

this line in specific

NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"User" inManagedObjectContext:context];

the .xcdatamodeld file i have is called UserData.xcdatamodeld and i have entity called "User".

To be honest i don;t know where in the code i should provide the UserData.xcdatamodeld file name?!! all i see in the examples is only the entity name!

the error i'm getting is:

[PopAdsAppDelegate managedObjectContext]: unrecognized selector sent to instance 0x180d60 2011-12-25 13:36:37.008 PopAds2[15645:707] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PopAdsAppDelegate managedObjectContext]: unrecognized selector sent to instance 0x180d60' * First throw call stack: (0x3435f8bf 0x345af1e5 0x34362acb 0x34361945 0x342bc680 0x3c87 0x41a9 0x35d6bc39 0x35cc36e9 0x35cc36b3 0x35cc35d5 0x323718a5 0x32366545 0x32366639 0x32366243 0x32366179 0x34333b03 0x343332cf 0x34332075 0x342b54dd 0x342b53a5 0x30b39fcd 0x37736743 0x305d 0x2ff4) terminate called throwing an exceptionkill

Upvotes: 0

Views: 2112

Answers (3)

thomas.g
thomas.g

Reputation: 3932

It seems that you have not synthesized your property

Add

@synthesize managedObjectContext;

to your AppDelegate.m file

EDIT I thought you just forgot to initialize your property but it seems that you are just beginning to learn CoreData.

The more efficient approach to learn how to initialize CoreData is to study the code of the defaut implementation provided by Apple. Create a new project and in the wizard chose to use CoreData. You will get a working code and see the two setters being overridden. At their first call the private attributes are initialized. This is lazy loading, initialize when first need it.

You should also read the CoreData starting point and especially CoreData programming guide

Upvotes: 1

Christian Kienle
Christian Kienle

Reputation: 753

Why don't you create a new project in Xcode and select the "Core Data" checkbox in the wizard? Xcode will then create a new project for you that uses Core Data. You usually don't have to setup Core Data yourself.

Upvotes: 1

Ariel
Ariel

Reputation: 2440

Your appDelegate have no managedObjectContext method as it seems from the error.

Edit:

Take a look at those links:
Ray Wenderlich Blog
Cocoa Dev Central
Mobile Tuts

And if those aren't enought, you could always Google it :)

Upvotes: 2

Related Questions