Objective-c – [NSManagedObject sayHello]: unrecognized selector sent to instance 0x

core-datansmanagedobjectobjective c

I try to extend NSManagedObject.
Using XCode I created MyBox.m and MyBox.h (directly from the xcdatamodel file).

Then I modified these files:

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


@interface MyBox : NSManagedObject

@property (nonatomic, retain) NSDate * endDate;
@property (nonatomic, retain) NSNumber * globalId;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSDate * startDate;

-(NSString *)sayHello;

@end

and

#import "MyBox.h"
@implementation MyBox

@dynamic endDate;
@dynamic globalId;
@dynamic name;
@dynamic startDate;

-(NSString *)sayHello {
    return @"hello";
}  

@end

I can fetch all myBoxes

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"MyBox" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];

NSMutableArray *myBoxes = [context executeFetchRequest:fetchRequest error:&error];

but later I call

MyBox *myBox = [myBoxes objectAtIndex:indexPath.row];    
    [myBox sayHello];

it compiles but then I get

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSManagedObject sayHello]: unrecognized selector sent to instance 0x8e73fc0'

If I only read a value like

NSLog(@"%@", myBox.name);

it works

I found similar problems here, but no solution.
Thanks for your help.

Best Answer

I've just got the same issue. Solved it by changing class name to the name of my NSManagedObject subclass in myApp.xcdatamodeld -> configurations -> default -> entities -> myEntity.

Related Topic