Cocoa:使用 NSSet 作为 NSMutableDictionary 中的键有什么缺点吗?
使用 NSSet
作为 NSMutableDictionary 中的键是否有任何缺点,需要注意的任何问题,是否有巨大的性能影响?
我认为键是在 Cocoa 容器中复制的,这是否意味着 NSSet 被复制到字典中?或者在这种情况下是否有一些保留 NSSet 的优化?
与 NSDictionary 可以接受 NSSet 作为键吗? 相关
示例代码:
NSMutableDictionary * dict = [NSMutableDictionary dictionary];
NSSet * set;
set = [NSSet setWithObjects:@"a", @"b", @"c", @"d", nil];
[dict setObject:@"1" forKey:set];
set = [NSSet setWithObjects:@"b", @"c", @"d", @"e", nil];
[dict setObject:@"2" forKey:set];
id key;
NSEnumerator * enumerator = [dict keyEnumerator];
while ((key = [enumerator nextObject]))
NSLog(@"%@ : %@", key, [dict objectForKey:key]);
set = [NSSet setWithObjects:@"c", @"b", @"e", @"d", nil];
NSString * value = [dict objectForKey:set];
NSLog(@"set: %@ : key: %@", set, value);
输出:
2009-12-08 15:42:17.885 x[4989] (d, e, b, c) : 2
2009-12-08 15:42:17.887 x[4989] (d, a, b, c) : 1
2009-12-08 15:42:17.887 x[4989] set: (d, e, b, c) : key: 2
Is there any downside to using NSSet
as key in NSMutableDictionary
, any gotchas to be aware of, any huge performance hits?
I think keys are copied in Cocoa containers, does it mean NSSet is copied to dictionary? Or is there some optimization that retains the NSSet in this case?
Related to Can a NSDictionary take in NSSet as key?
Example code:
NSMutableDictionary * dict = [NSMutableDictionary dictionary];
NSSet * set;
set = [NSSet setWithObjects:@"a", @"b", @"c", @"d", nil];
[dict setObject:@"1" forKey:set];
set = [NSSet setWithObjects:@"b", @"c", @"d", @"e", nil];
[dict setObject:@"2" forKey:set];
id key;
NSEnumerator * enumerator = [dict keyEnumerator];
while ((key = [enumerator nextObject]))
NSLog(@"%@ : %@", key, [dict objectForKey:key]);
set = [NSSet setWithObjects:@"c", @"b", @"e", @"d", nil];
NSString * value = [dict objectForKey:set];
NSLog(@"set: %@ : key: %@", set, value);
Outputs:
2009-12-08 15:42:17.885 x[4989] (d, e, b, c) : 2
2009-12-08 15:42:17.887 x[4989] (d, a, b, c) : 1
2009-12-08 15:42:17.887 x[4989] set: (d, e, b, c) : key: 2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NSDictionaries 会复制它们的键。
不可变集可能会通过返回保留的自身来响应复制,从而使“复制”实际上是免费的。
可变集将通过返回其自身的副本来响应复制,这就是为什么使用可变对象作为键通常是一个坏主意(在改变它之后你将无法找到原始对象,因为它不再比较等于字典中的键)。
NSDictionaries do copy their keys.
An immutable set will probably respond to
copy
by returning itself retained, making the “copy” practically free.A mutable set will respond to
copy
by returning a copy of itself, which is why using mutable objects as keys is generally a bad idea (you won't be able to find the original after mutating it because it no longer compares equal to the key in the dictionary).哦。是的。性能存在很大的缺点。碰巧
-[NSSet hash]
被实现为[set count]
。这意味着,如果您的所有集合都有 2 个对象,那么它们都具有相同的哈希值,并且集合的性能将非常差。Ooh. Yes. There is a big performance downside. It happens that
-[NSSet hash]
is implemented as[set count]
. That means that if all your sets have 2 objects, say, then they all have the same hash, and the collection will perform very poorly.