Cocoa:使用 NSSet 作为 NSMutableDictionary 中的键有什么缺点吗?

发布于 2024-08-13 08:48:04 字数 1094 浏览 12 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我们的影子 2024-08-20 08:48:04

我认为键是在 Cocoa 容器中复制的,这是否意味着 NSSet 被复制到字典中?或者在这种情况下是否有一些保留 NSSet 的优化?

NSDictionaries 会复制它们的键。

不可变集可能会通过返回保留的自身来响应复制,从而使“复制”实际上是免费的。

可变集将通过返回其自身的副本来响应复制,这就是为什么使用可变对象作为键通常是一个坏主意(在改变它之后你将无法找到原始对象,因为它不再比较等于字典中的键)。

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?

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).

依 靠 2024-08-20 08:48:04

哦。是的。性能存在很大的缺点。碰巧 -[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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文