objective-c - 将对象添加到具有多对多关系的核心数据中的 NSSet

标签 objective-c ios core-data many-to-many

我有一个包含 Restaurants 和 Atmospheres 的核心数据结构,其中一个餐厅可以有很多氛围,一个氛围可以有很多餐厅。因此,我建立了两个对多关系,两者都与 Apple 文档中所述的自身相反,形成了一个多对多关系。

但是,我无法将对象添加到创建的集合中。例如,当我使用这样的代码时,

Atmosphere *atmosphere = [Atmosphere atmosphere:aId inManagedObjectContext:context];
[restaurant addAtmospheresObject:atmosphere];

它因奇怪的错误而崩溃:

EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)

有没有人遇到过这种情况?

最佳答案

看起来您没有正确创建atmosphere 对象。试试这个:

Atmosphere *atmosphere = [NSEntityDescription 
   insertNewObjectForEntityForName:@"Atmosphere" 
            inManagedObjectContext:context];
// further configuration
if (restaurant) {
   [restaurant addAtmospheresObject:atmosphere];
}

关于objective-c - 将对象添加到具有多对多关系的核心数据中的 NSSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10056014/

相关文章:

objective-c - 在一对一关系的核心数据删除过程中查找 NSManagedObject 时出现问题

ios - 具有计时器的 UITableViewCell

ios - 处理核心数据中的大量删除/插入

objective-c - 如何在 iOS 中通过参数将 UITableView IndexPath 传递给 UIButton @selector?

objective-c - 如何将浮点值数组写入 Core Audio 中的音频文件?

ios - 使用滑动手势更改 iOS View / View Controller

iphone - NSPredicate 格式等同于 SQL 查询

ios - 如何使用 xmpp 在 swift 中按日期对聊天消息进行分组?

iphone - iOS iPhone 在空间中显示用户方向和方向,如 MKMapView 上的指南针应用程序

ios - iOS 中的自动 OTP 验证?