在实例上调用基类方法时出现“无法识别的选择器”异常并且看不到问题所在。
我有一个名为 Form 的对象,如下所示:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import "HPSDbBase.h"
@interface Form : HPSDbBase
@end
Form 的基类如下所示:
#import <CoreData/CoreData.h>
@interface HPSDbBase : NSManagedObject
@property (nonatomic, retain) NSString * id;
@property (nonatomic, retain) NSString * json;
-(id)getJSONElement:(NSString*)key;
@end
然后我尝试在视图控制器方法中使用 Form 对象,如下所示:
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
}
例外是:
-[NSManagedObject getJSONElement:]: unrecognized selector sent to instance 0x8290940
谁能看到我做错了什么?
太感谢了!
编辑 1
这是 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