问题
I am getting an 'unrecognised selector' exception when calling a base-class method on an instance and can't see what the problem is.
I have an object called Form as follows:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import "HPSDbBase.h"
@interface Form : HPSDbBase
@end
The base class for Form looks like this:
#import <CoreData/CoreData.h>
@interface HPSDbBase : NSManagedObject
@property (nonatomic, retain) NSString * id;
@property (nonatomic, retain) NSString * json;
-(id)getJSONElement:(NSString*)key;
@end
I then try using the Form object within a view controller method as follows:
HPSAppDelegate* appDelegate = [[UIApplication sharedApplication] delegate];
NSError* error = nil;
NSFetchRequest * request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Form" inManagedObjectContext:appDelegate.managedObjectContext]];
NSArray* arrayOfForms = [appDelegate.managedObjectContext executeFetchRequest:request error:&error];
for (int i=0;i<arrayOfForms.count;i++)
{
Form* dbForm = [arrayOfForms objectAtIndex:i];
NSLog(@"Form.json=%@",dbForm.json); // this works
NSString* wwwww = (Form*)[dbForm getJSONElement:@"test"]; // exception here
}
The exception is:
-[NSManagedObject getJSONElement:]: unrecognized selector sent to instance 0x8290940
Can anyone see what I'm doing wrong?
Thanks a million!
EDIT 1
Here is the implementation for HPSDbBase:
#import "HPSDbBase.h"
@implementation HPSDbBase
@dynamic id;
@dynamic json;
-(id)getJSONElement:(NSString*)key
{
NSData *jsonData = [[self json] dataUsingEncoding:NSUTF8StringEncoding];
NSError *e = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options: NSJSONReadingMutableContainers error: &e];
NSDictionary *jsonDictionary = (NSDictionary *)jsonObject;
id rc = [jsonDictionary objectForKey:key];
return rc;
}
@end
回答1:
I tracked down the problem.
I had renamed my core-data object. I renamed everything I could see regarding the name of the core-data object, but it was obviously not enough. I deleted the core-data entity, then recreated a brand new one with the right name and everything started working.
回答2:
I had also received this error after renaming a class.
If you prefer not to delete your classes, I found that I could resolve the error by opening up my "xcdatamodeld" file, and clicking on Configurations -> Default. There, the entity's Class description was still referencing the old name. After correcting it here, the problem was resolved.
回答3:
I also received this error. It transpired that it was simply that there was a typo in the name of the attribute in the core data configuration of the entity. It was named correctly in the @dynamic statement in the class implementation so didn't raise a flag when compiled but did as soon as accessed. Because I was setting up NSManagedObject sub-Entities I was distracted from looking for the obvious. Just listing this here in case someone else is in the same boat.
来源:https://stackoverflow.com/questions/11344891/unrecognized-selector-for-nsmanagedobject-base-class