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
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.