NSManagedObject基类的无法识别的选择器 [英] Unrecognized selector for NSManagedObject base class
问题描述
我在实例上调用基类方法时遇到无法识别的选择器异常,无法看到问题所在。
我有一个对象调用Form如下:
#import< Foundation / Foundation.h>
#import< CoreData / CoreData.h>
#importHPSDbBase.h
@interface表单: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];
[请求setEntity:[NSEntityDescription entityForName:@ForminManagedObjectContext:appDelegate.managedObjectContext]];
NSArray * arrayOfForms = [appDelegate.managedObjectContext executeFetchRequest:request error:& error];
for(int i = 0; i {
Form * dbForm = [arrayOfForms objectAtIndex:i];
NSLog(@Form.json =%@,dbForm.json); // this works
NSString * wwwww =(Form *)[dbForm getJSONElement:@test]; // exception here
}
例外是:
- [NSManagedObject getJSONElement:]:无法识别的选择器发送到实例0x8290940
任何人都能看到我做错了什么?
感谢一百万!
EDIT 1
以下是HPSDbBase的实现:
#importHPSDbBase.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 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.
这篇关于NSManagedObject基类的无法识别的选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!