【问题标题】:Adding an object to an NSSet in a Core Data with many-to-many relationships将对象添加到具有多对多关系的核心数据中的 NSSet
【发布时间】:2012-04-07 16:07:38
【问题描述】:

我有一个包含餐厅和氛围的核心数据结构,其中一个餐厅可以有很多氛围,一个氛围可以有很多餐厅。所以我建立了两个多对多的关系,两者都是苹果文档中所述的逆向关系,形成了一个多对多的关系。

但是,我在将对象添加到创建的集合时遇到了麻烦。例如,当我使用这样的代码时,

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

它因一个奇怪的错误而崩溃:

EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)

请问有人遇到过吗?

【问题讨论】:

  • 什么是大气 *atmosphere = [大气大气:aId inManagedObjectContext:context];?这不是有效的语法..
  • 我在 Atmosphere 实体中有一个类方法(使用类别)+(Atmosphere *)atmosphere:(NSNumber *)unique inManagedObjectContext:(NSManagedObjectContext *)context;它通过一个唯一的 id 字段从核心数据中获取一个 Atmosphere 对象
  • 这个类方法后面的大气变量是非nil吗?
  • 那是愚蠢的,不,不是!谢谢你的帮助!
  • 为您的努力+1。 :-)

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


【解决方案1】:

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

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

【讨论】:

  • 欢迎 - 也可以试试投票按钮...不要忘记勾选复选标记。
猜你喜欢
  • 1970-01-01
  • 2016-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多