5

We have a bunch of NSManagedObjects of various types. Some of them have members that are NSSet's of other NSManagedObjects. The problem is that I really need to override the hash and isEquals methods of the objects that are IN the set - but they are NSManagedObjects. I'm having problems with getting multiple identical objects in the set. As far as I can tell, since hash defaults to the object address - all objects are different. So I need to override hash and isEquals - but can't see any way to do it.

What we have is a bunch of stuff in the System, and more comes in via XML - sometimes repeats of the existing objects. When they are the same, I don't want dups added to the set.

4

1 回答 1

1

正如 Wain 上面提到的,NSManagedObject 文档声明你不能覆盖hashisEqual:。所以这意味着一只股票NSSet不能满足你的需要。

您的一些选择是:

  • 枚举NSSet内容以识别和删除重复项
  • 为您编写一个工厂方法,NSManagedObjects当给定相同的输入时,它将返回相同的对象
  • 修复 XML 以不包含重复的对象
  • 来自 XML 的对象在它们变为之前唯一NSManagedObjects
  • 修改输入 XML 以包含您可以跟踪的唯一标识符,假设重复的对象是完全重复的
  • 实现你自己的类似 NSSet 的集合类,它执行不同于hash和的唯一测试isEqual:
于 2013-09-28T09:56:19.287 回答