Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[NSManagedObject sayHello]: unrecognized selector sent to instance 0x

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.

like image 255
stefanr Avatar asked Jul 09 '13 10:07

stefanr


3 Answers

For Swift 5.0

This issue is present when you create the CoreData object in this way:

let object = CoreDataClass()
print(object.someProperty) // this is emit crash
like image 189
Au Room Avatar answered Nov 07 '22 07:11

Au Room


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.

like image 30
Alexander Vasenin Avatar answered Nov 07 '22 06:11

Alexander Vasenin


Assuming you have set the class name correctly on the MyBox entity, I would guess that the app has an older version of your Core Data managed object model. Clean your build and delete the app on the simulator/device for good measure. To be 100% sure, also delete your derived data folder.

If it doesn't work after that, I'll bet that you haven't set the entity class name correctly. Print out your NSEntityDescription and make sure it is what you expect.

like image 4
Drewsmits Avatar answered Nov 07 '22 08:11

Drewsmits