NSManagedObject作为NSDictionary键? [英] NSManagedObject as NSDictionary key?

查看:204
本文介绍了NSManagedObject作为NSDictionary键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个 NSDictionary ,其键应该是 NSManagedObject 子类的实例。

In my app, I have a NSDictionary whose keys should be instances of a subclass of NSManagedObject.

但是,问题是 NSManagedObject 没有实现 NSCopying 协议,这意味着 NSManagedObject 的核心数据对象/实例可以用作字典键,即使 - [hash] code>方法对他们很好。

The problem, however, is that NSManagedObject does not implement the NSCopying protocol which means that no Core Data objects / instances of NSManagedObject can be used as dictionary keys even though the -[hash] method works fine for them.

我应该怎么办?

推荐答案

有四个选项:


  1. 使用不同的对象作为字典键,然后从中查找。 [object objectID] + [NSValue valueWithNonretainedObject:] 似乎最明显

  2. 使用 CFDictionaryCreateMutable()创建一个具有保留键的字典,而不是复制,然后调用 CFDictionarySetValue ()以存储对象

  3. 在OS X或iOS6 +上, [NSMapTable mapTableWithStrongToStrongObjects] 纯Objective-C相当于 CFMutableDictionary

  4. 为您的托管对象实现 NSCopying 子类,以便它返回self(如果您不使用ARC,则带有引用计数)

  1. Use a different object as the dictionary key instead, and lookup from that. [object objectID] or +[NSValue valueWithNonretainedObject:] seem the most obvious
  2. Use CFDictionaryCreateMutable() to create a dictionary with retained keys, rather than copied, instead, and then call CFDictionarySetValue() to store the objects
  3. On OS X or iOS6+, [NSMapTable mapTableWithStrongToStrongObjects] gives you a purely Objective-C equivalent to CFMutableDictionary
  4. Implement NSCopying for your managed object subclass, such that it returns self (with a bumped reference count if you're not using ARC)



注意



+ valueWithNonretainedObject:是非常危险的,因为它可能留下悬空指针;

Notes

+valueWithNonretainedObject: is pretty dangerous, since it's possible to be left with a dangling pointer; likely best to avoid.

存储对象ID很好,除了对象开始使用临时 / em> ID。当上下文保存到磁盘时(或 -obtainPermanentIDsForObjects:... 被调用),该ID将更改为永久ID。

Storing object IDs is fine, apart from the fact that new objects start out life with a temporary ID. That ID then changes to a permanent one when the context is saved to disk (or -obtainPermanentIDsForObjects:… is called). Your mapping code needs to be smart enough to handle this unless it can guarantee that all incoming objects already have a permanent ID.

实施 NSCopying

Implementing NSCopying like this feels a bit icky, but should work just fine. As it happens, this is exactly the approach NSURLSessionTask takes, I presume for dictionary friendliness.

在OS X之前10.8 Mountain Lion,以前可以创建一个常规的 NSMutableDictionary ,然后调用 CFDictionarySetValue()。这不是这样的;新的字典现在具有在CF级指定的适当的复制回调,而不是纯粹是 NSMutableDictionary 的特征。

Prior to OS X 10.8 Mountain Lion, it used to be possible to create a regular NSMutableDictionary and then call CFDictionarySetValue() for it. That's no longer the case though; new dictionaries now have proper copy callbacks specified down at the CF level, rather than purely being a feature of NSMutableDictionary.

这篇关于NSManagedObject作为NSDictionary键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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