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

查看:148
本文介绍了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 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]];

(当我回来再给你这个键对象实例时, out。)

("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];

让我们假设字典没有复制键,而是保留。如果现在,在稍后的点,原始字符串被修改,那么很可能你不会再次在字典中找到你的存储值,即使你使用非常相同的键对象(即, 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.

注意,简单到足以定义 -copyWithZone: code> return [self retain] 。这是允许的和好的代码,如果你的对象是不可变的,并且 NSCopying 契约是专门设计的,返回的对象必须是(sorta,kinda)immutable: / 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天全站免登陆