我在一个名为“Item”的 CoreData 实体中有简单的树结构。
任何项目都可以是另一个项目的父项,并且可以有多个子项。
实体中有以下关系:
childItems:To-Many,目的地:Item,逆:parentItem。可选的 parentItem:目标:项目,反向:childItems。可选的
我正在使用 Xcode 生成的以下 Item.h :
@interface Item : NSManagedObject
{
}
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) Item * parentItem;
@property (nonatomic, retain) NSSet* childItems;
@end
@interface Item (CoreDataGeneratedAccessors)
- (void)addChildItemsObject:(Item *)value;
- (void)removeChildItemsObject:(Item *)value;
- (void)addChildItems:(NSSet *)value;
- (void)removeChildItems:(NSSet *)value;
@end
获取请求非常简单:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Item" inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects: sort, nil]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:[self managedObjectContext] sectionNameKeyPath:nil
cacheName:@"AllItemsCache"];
当我尝试设置关系时,它只会崩溃。为什么?
Item *item = [itemFetchedResultsController.fetchedObjects objectAtIndex:0];
[item addChildItemsObject:childItem];
调试输出:
*** -[Item addChildItemsObject:]: unrecognized selector sent to instance 0x5494140
(gdb) po 0x5494140
<Item: 0x5494140> (entity: Item; id: 0x54921d0 <x-coredata://AB862620-04BE-4E42-84A6-8723455F5957/Item/p1> ; data: {
title = Test123;
})
我可以设置或获取其他属性就好了。但是当我试图改变关系时它崩溃了。我究竟做错了什么?