使用NSArrayController绑定有序关系 [英] Binding an Ordered Relationship with an NSArrayController

查看:221
本文介绍了使用NSArrayController绑定有序关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将NSArrayController的内容绑定到有序对多关系中的实体?

我有一个无序的多对多关系,以及一个NSArrayController,其内容集从父实体绑定到该关系。这工作正常,数据可以从NSArrayController访问,没有问题。

I have an unordered to-many relationship in my Core Data model, and an NSArrayController whose Content Set is bound to that relationship from the parent entity. This works fine, the data is accessible from the NSArrayController, no problem.

我决定在开发期间,最好允许用户手动重新排序这些子对象,我改变了一个有序的关系。现在,当我的NSArrayController是从我的nib创建时,会出现以下错误:

I decided during development that it would be better to allow users to manually reorder these child objects, so I changed the relationship to an ordered one. Now, when my NSArrayController is being created from my nib, the following error is presented:

Cannot create NSSet from object Relationship '...' fault on managed object ... of class _NSFaultingMutableOrderedSet

现在实际上,感觉:这是一个有序的关系,所以现在我得到一个有序集。绑定到内容数组也是不合适的,因为它现在是一个NSOrderedSet,而不是一个数组。我的问题是:现在如何将这个关系的数据绑定回NSArrayController?

Now actually, I think this all makes sense: It's an ordered relationship, so now I'm getting an ordered set. Binding it to Content Array also would be inappropriate, since it's now an NSOrderedSet, not an array. My question is: Now how do I bind this relationship's data back into the NSArrayController?

推荐答案

根本的问题是Core Data有序对多关系返回NSOrderedSet,NSOrderedSet不是NSSet的子类。任何期望NSSet的数组控制器绑定都会失败。

The fundamental problem is that a Core Data ordered to-many relationship returns an NSOrderedSet, and NSOrderedSet is not a subclass of NSSet. Any array controller bindings that expect an NSSet will fail.

Tom Fewster有一个详细的博客文章描述了使用NSValueTransformer来解决这个缺点,在NSOrderedSet和NSArray之间进行转换。他还提供了 Github上的示例实现

Tom Fewster has a detailed blog post describing the use of NSValueTransformer to work around this shortcoming, converting between NSOrderedSet and NSArray on the fly. He also provides a sample implementation on Github.

这篇关于使用NSArrayController绑定有序关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆