【问题标题】:Core Data To Many Peer Relationship核心数据与许多对等关系
【发布时间】:2013-09-13 02:50:24
【问题描述】:

我正在为食谱编写一个基于核心数据的 Cocoa 应用程序。我有一个成分实体,并且想要创建一个与其他成分的成分替代品关系,但是在设置关系或保存我无法弄清楚的商店时出现错误。这是实体描述:

Ingredient
Attributes:
  ingredientName type:String
Relationships:
  ingredientSubstitutes destination:Ingredient inverse:ingredientSubstitutes

在我的 Nib 中,我有 3 个阵列控制器:

  • 所有成分 AC
  • 可用的替代品 AC
  • 精选成分替代 AC

我有 3 个表视图,每个视图都显示这些数组控制器的内容。然后我有一个按钮来添加一种成分作为另一种成分的替代品,绑定如下

Button bindings
Target: All Ingredients AC.selection 
        Selector Name: addIngredientSubstitutesObject:
Argument: Available Substitutes AC.seletion

使用此设置,只要我单击添加按钮,应用程序就会抛出无法识别的选择器发送到实例异常:“-[_NSControllerObjectProxy 实体]:无法识别的选择器发送到实例”,好像成分无法识别 addIngredientSubstitutesObject。我添加了一个代理方法,以确保这是无法识别的选择器,这确实是问题所在。

在尝试了一堆东西但没有任何地方之后,作为一个实验,我改变了模型,所以成分替代品没有逆:

Ingredient
Attributes:
  ingredientName type:String
Relationships:
  ingredientSubstitutes destination:Ingredient inverse:*none*

当我运行它时,添加成功,所有表都会相应更新,但在保存时,我得到一个不同的无法识别的选择器,并且应用程序抛出异常:

-[_NSControllerObjectProxy _isKindOfEntity:]:无法识别的选择器发送到实例

关于可能发生的事情有什么建议吗?我是否采取了错误的方法?

【问题讨论】:

    标签: objective-c core-data cocoa-bindings


    【解决方案1】:

    想通了,这个帖子帮我提示了:Core Data Programmatically Adding a to-many Relationship

    基本上,将目标绑定到 Array Controller 的选择意味着目标对象是 Array Controller 的 Proxy 对象的成分,而不是实际的成分,显然不会响应 To Many 访问器那个成分可以。我通过在应用程序委托中实现一个方法来解决这个问题,该方法获取实际对象并可以使用 To Many 访问器:

    - (void)addSubstituteForSelectedIngredient:(OFIngredient *)ingredient
    {
      OFIngredient *selectedIngredient = [[self.allIngredientsArrayController selectedObjects] objectAtIndex:0];
      OFIngredient *selectedSubstitute = [[self.availableSubsArrayController selectedObjects] objectAtIndex:0];
    
      [selectedIngredient addIngredientSubsObject:selectedSubstitute];
    }
    

    注意 NSArrayController 的 -(id) 选择方法(我之前试过,原描述中提到过)返回一个代理对象,所以必须使用 (NSArray *)selectedObjects!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-02
      • 2016-06-30
      • 1970-01-01
      • 1970-01-01
      • 2014-08-13
      • 1970-01-01
      相关资源
      最近更新 更多