我想從empId,Name,deptId作爲屬性的'Employee'Entity獲取數據。將ManagedObject轉換爲NSObject
// 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];
但得到了同樣的崩潰。
沒有得到它爲什麼導致問題。我有方法定義&在適當的地方執行&它甚至沒有在編譯時給我警告。
錯字嘗試 - 應該是'如果(managedObject!)'... – Mundi
managedObject我想過去不爲空,但是,我m到處objectProperties爲NULL&objectsCount爲0是這意味着我的managedObject是空的。我做什麼 – JiteshW
Jitesh - 希望你已經糾正了我的錯字,正如Mundi提到的 – thatzprem