我正在尝试从具有 empId、Name、deptId 作为属性的“员工”实体中获取数据。
// Used to Populate data in table
Employee : NSObject
@property (nonatomic, retain) NSString *name;
@property int empId;
@property int deptId;
@synthesize empId, name, deptId;
&
CDEmployee : NSManagedObject
@property (nonatomic, retain) NSString *name;
@property int empId;
@property int deptId;
- (void)convertMyData:(Employee *)emp;
@dynamic empId, name, deptId;
- (void)convertMyData:(Employee *)emp
{
self.empId = emp.empId;
self.name = emp.name;
self.deptId = emp.deptId;
}
// My code to fetch & convert data retured from db to Employee class
-(NSArray *)getAllEmployees:(NSManagedObjectContext*)context
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(empId > %@)",[NSNumber numberWithInt:-1],[NSNumber numberWithInt:1]];
NSFetchRequest* req = [self createRequest:context]; // Request is correct
[req setPredicate:predicate];
NSFetchedResultsController* fetchContr = [[NSFetchedResultsController alloc] initWithFetchRequest:req managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];
NSError* error = nil;
[fetchContr performFetch:&error];
NSArray *fetchedObjects = [fetchContr fetchedObjects];
NSMutableArray *result = [[[NSMutableArray alloc] init] autorelease];
for(int i = 0; i < [fetchedObjects count]; i++)
{
CDEmployee *cdEmp = [[fetchedObjects objectAtIndex:i] retain]; // just tried retaing
if (cdEmp)
{
Employee *emp = [[Employee alloc] init];
NSLog(@"Employee - %@", emp); // Shows tht is has object
[cdEmp convertMyData:emp]; //-----> Crashing here
[result addObject:emp];
[emp release];
}
}
[fetchContr release];
return result;
}
我正在从核心数据中获取结果并得到正确的结果,但是当我将核心数据结果转换回Employee(NSObject)
类时,我将[NSManagedObject convertMyData:]:
无法识别的选择器发送到实例。
我尝试向类添加另一种方法-(void)helloWorld
并CDEmployee
尝试过[cdEmp helloWorld];
,但遇到了同样的崩溃。
不明白为什么它会导致问题。我已经在适当的地方定义和实现了方法,它甚至没有在编译时给我警告。