为什么 CoreData 因“无法识别的选择器”而崩溃?当我尝试建立关系时?

发布于 2024-09-08 05:23:15 字数 1900 浏览 6 评论 0原文

我在一个名为“Item”的 CoreData 实体中有简单的树结构。

任何项目都可以是另一个项目的父项,并且可以有多个子项。

实体中有以下关系:

childItems:To-Many,Destination:Item,inverse:parentItem。选修的 父项:目标:项,反向:子项。可选

我正在使用 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;
})

我可以很好地设置或获取其他属性。但当我试图改变人际关系时,它就崩溃了。 我做错了什么?

I have simple tree structure in one CoreData entity called "Item".

Any item can be parent of another item and can have multiple children.

There's following relationships in the entity:

childItems: To-Many, Destination: Item, inverse: parentItem. optional
parentItem: Destination: Item, inverse: childItems. optional

I am using Xcode-generated following 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

The fetch request is very straightforward:

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"];

When I try to set relationship it simply crashes. Why?

Item *item = [itemFetchedResultsController.fetchedObjects objectAtIndex:0];
[item addChildItemsObject:childItem];

debug output:

*** -[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;
})

I can set or get other properties just fine. but when I try to change relationships it crashes.
What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伴梦长久 2024-09-15 05:23:15

我猜想这是由于使用“Item”这样的通用词作为类名而引起的命名冲突。尝试将名称重构为更独特的名称。

或者,您可以尝试验证该类是否响应方法:

Item *item = [itemFetchedResultsController.fetchedObjects objectAtIndex:0];
NSLog(@"responds=%@",[item respondsToSelector:@selector(addChildItemsObject:)] ?@"YES":@"NO");
[item addChildItemsObject:childItem];

或调查直接返回的对象的类:

NSLog(@"class=%@",[[itemFetchedResultsController.fetchedObjects objectAtIndex:0] class]);

您实际上可能返回一个正在默默地转换为其 Item 子类的通用 NSManagedObject。不太可能,但你不妨检查一下。

I would guess it is a naming collision caused by using a generic word like "Item" as a class name. Try refactoring the name to something more unique.

Alternatively, you might try verifying that the class does respond to method:

Item *item = [itemFetchedResultsController.fetchedObjects objectAtIndex:0];
NSLog(@"responds=%@",[item respondsToSelector:@selector(addChildItemsObject:)] ?@"YES":@"NO");
[item addChildItemsObject:childItem];

or investigate the class of the object returned directly:

NSLog(@"class=%@",[[itemFetchedResultsController.fetchedObjects objectAtIndex:0] class]);

It's possible that your actually returning a generic NSManagedObject that is being silently cast to its Item subclass. Unlikely, but you might as well check.

临走之时 2024-09-15 05:23:15

找到了问题的原因。

CoreData 模型和数据类仅在您的项目内部才有效。
如果您将它们放入依赖项目中,则会出现如上所述的奇怪错误。

我的 .xcdatamodel 和实体类位于其他项目中,并且其他项目添加到主项目中。

一旦我将所有内容移至主项目并删除依赖项,它就可以正常工作。

Found the cause of the problem.

CoreData model and data classes only work if they are inside your project.
IF you put them into dependent project it will have the strange errors like above.

My .xcdatamodel and entity classes was in other project and other project added to main project.

As soon as I moved everything into main project and removed dependency it works fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文