0

I have a basic Core Data model which look like this : enter image description here

I auto-generated data model files and it's give me something like that :

BSStudent.h

//
//  BSStudent.h
//

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class BSFormation;

@interface BSStudent : NSManagedObject

@property (nonatomic, retain) NSString * firstname;
@property (nonatomic, retain) NSString * lastname;
@property (nonatomic, retain) NSDate * birthdate;
@property (nonatomic, retain) id thumbnail;
@property (nonatomic, retain) NSSet *formations;
@end

@interface BSStudent (CoreDataGeneratedAccessors)

- (void)addFormationsObject:(BSFormation *)value;
- (void)removeFormationsObject:(BSFormation *)value;
- (void)addFormations:(NSSet *)values;
- (void)removeFormations:(NSSet *)values;

@end

BSStudent.m

//
//  BSStudent.m
//

#import "BSStudent.h"
#import "BSFormation.h"


@implementation BSStudent

@dynamic firstname;
@dynamic lastname;
@dynamic birthdate;
@dynamic thumbnail;
@dynamic formations;

@end

I tried to simply save a BSStudent object by doing the following :

BSStudent *newStudent = (BSStudent *)[NSEntityDescription entityForName:kBSStudent inManagedObjectContext:self.managedObjectContext];
    newStudent.firstname = firstname;
    newStudent.lastname = lastname;
    newStudent.birthdate = birthdate;
    newStudent.thumbnail = thumbnail;

    NSError *error;
    if (![self.managedObjectContext save:&error]) {
        NSLog(@"Error: %@", [error localizedDescription]);
    }

But my app always crash with error : [NSEntityDescription setFirstname:]: unrecognized selector sent to instance 0x1f021e00

Someone figuring out what's happening here ?

Yaman
  • 3,949
  • 4
  • 38
  • 60

1 Answers1

1

One possible explanation is that you are using the wrong string in:

BSStudent *newStudent = (BSStudent *)[NSEntityDescription entityForName:kBSStudent inManagedObjectContext:self.managedObjectContext];

Try and check what newStudent actually is:

BSStudent *newStudent = (BSStudent *)[NSEntityDescription entityForName:kBSStudent inManagedObjectContext:self.managedObjectContext];
NSLog(@"New Student: %@", [newStudent description]);
newStudent.firstname = firstname;

maybe it will clarify things...

sergio
  • 68,819
  • 11
  • 102
  • 123
  • seems good with `newStudent.description` : `2013-01-26 16:49:34.641 Bulletin scolaire[3203:907] () name BSStudent, managedObjectClassName BSStudent, renamingIdentifier BSStudent, isAbstract 0, superentity name (null) [...]` – Yaman Jan 26 '13 at 15:51
  • maybe you are using an old version of your model, for some reason... have you tried cleaning the project, deleting the app from the device, and rebuild/run? – sergio Jan 26 '13 at 16:07
  • just tested to clean project + deleting app from device => same result :( – Yaman Jan 26 '13 at 16:12
  • 1
    I found out where was the problem. I was using `[NSEntityDescription entityForName:...]` instead of `[NSEntityDescription insertNewObjectForEntityForName:...]`. Thx for your help sergio – Yaman Jan 26 '13 at 16:38