Cocoa的NSDictionary:为什么复制密钥? [英] Cocoa's NSDictionary: why are keys copied?

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

问题描述

在NS(Mutable)字典中用作键的所有对象都必须支持NSCopying协议,这些对象在字典中使用时被复制。

All objects used as keys in NS(Mutable)Dictionaries must support the NSCopying protocol, and those objects are copied when they're used in the dictionary.

I经常想要使用较重的体重对象作为键,只需将一个对象映射到另一个对象。当我这样做时,我真正的意思是:

I frequently want to use heavier weight objects as keys, simply to map one object to another. What I really mean when I do that is effectively:

[dictionary setObject:someObject forKey:[NSValue valueWithPointer:keyObject]];

(当我回来,再次给你这个相同的关键对象实例时,给我一个相同的值)

("When I come back and hand you this same key object instance again, get me that same value out.")

...这正是我最后做的,有时会绕过这个设计。 (是的,我知道在桌面Cocoa中的NSMapTable;但是例如iPhone不支持这一点。)

...which is exactly what I end up doing to get around this design sometimes. (Yes, I know about NSMapTable in desktop Cocoa; but e.g. iPhone doesn't support this.)

但是我不是真的得到的是为什么复制密钥是首要的必要或可取的。它购买的是执行还是来电?

But what I don't really get is why copying the key is necessary or desirable in the first place. What does it buy the implementation or caller?

推荐答案

该副本确保用作键的值在用作键时不会改变underhand。考虑可变字符串的示例:

The copy ensures, that the values used as keys don't change "underhand" while being used as keys. Consider the example of a mutable string:

NSMutableString* key = ... 
NSMutableDictionary* dict = [[NSMutableDictionary alloc] init];

[dict setObject: ... forKey: key];

我们假设字典没有复制密钥,而只是保留。如果现在,在稍后的一点,原始字符串被修改,那么很可能你不会再次在字典中找到你的存储值,即使你使用相同的键对象(即,

Let's assume, that the dictionary did not copy the key, but instead just retained it. If now, at some later point, the original string is modified, then it is very likely, that you are not going to find your stored value in the dictionary again, even if you use the very same key object (i.e., the one, key points to in the example above).

为了保护自己免受这样的错误,字典副本所有的键。

In order to protect yourself against such a mistake, the dictionary copies all keys.

请注意,btw。它只是简单的定义 -copyWithZone: code> return [self retain] 。这是允许的和良好的代码,如果您的对象是不可变的,并且 NSCopying 合同是专门设计的,则返回的对象必须是(sorta,kinda)不可变的: / p>

Note, btw., that it is simple enough to define -copyWithZone: as just doing return [self retain]. This is allowed and good code, if your object is immutable, and the NSCopying contract is specifically designed such, that the object returned has to be (sorta,kinda) immutable:


通过保留原始代码,而不是在课程及其内容不可变时创建新的副本来实施NSCopying。

Implement NSCopying by retaining the original instead of creating a new copy when the class and its contents are immutable.


如果考虑immutable vs.可变适用于接收对象;否则副本的确切性质由课程确定。

The copy returned is immutable if the consideration "immutable vs. mutable" applies to the receiving object; otherwise the exact nature of the copy is determined by the class.

(均来自 NSCopying参考

即使您的对象不是不可变的,如果您只使用基于身份的等同/哈希实现,即实现,也不会受到对象内部状态的任何影响,您可能会忽略该实现。

Even if your objects are not immutable, you might get away with that implementation, if you only ever use identity-based equality/hash implementations, i.e., implementations, which are ot affected in any way by the object's internal state.

这篇关于Cocoa的NSDictionary:为什么复制密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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