我正在使用CoreData並定義了一個實體ContactList,它是使用XCode xcdatamodel工具(或任何您稱之爲內置事物:)創建的。我還將實體的類名稱設置爲「ContactList」。然後我使用rentzsch的mogenerator(http://github.com/rentzsch/mogenerator)生成我的自定義類文件,並將簡單方法「toString」添加到我的ContactList類中。NSFetchedResultsController總是返回NSManagedObject對象,而不是自定義
的fetchedResultsController回調是相當多的標準,如下所示:
- (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController_ != nil) {
return fetchedResultsController_;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"ContactList" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
NSError *error = nil;
if (![fetchedResultsController_ performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return fetchedResultsController_;
}
現在,當我開始我的應用程序並嘗試調用該方法在「ContactList」對象,這反過來我從獲得NSFetchedResultsController,我得到
ContactList *contactList = (ContactList *) [self.fetchedResultsController objectAtIndexPath:indexPath];
[contactList toString];
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSManagedObject toString]: unrecognized selector sent to instance 0x6af7bf0'
由於消息顯示,不知何故NSFetchedResultsController不返回「ContactList」對象,但更普遍的NSManagedObject來代替。
有誰知道爲什麼以及如何解決這個問題?
你的fetchedResultsController的實現是什麼樣的?如果提取請求錯誤(例如,錯誤的NSEntityDescription,錯誤的謂詞語法等),那麼您可能不會得到正確類型的管理對象。此外,如果你只是NSLog()contactList對象而不是調用toString就會發生什麼呢? – Echelon 2010-09-20 15:20:40
嘿埃施朗,我添加了我的fetchedResultsController實現,但它非常標準。 NSLog(@「%@」,contactList)給我:(實體:ContactList; id:0x6a04180 ; data: ) –
Sebastian
2010-09-20 15:32:12