NSDictionary可以接受NSSet作为键吗? [英] Can a NSDictionary take in NSSet as key?

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

问题描述

我知道你可以使用任何对象作为NSDictionary的关键,但问题是它能够检索正确的值吗?支持我有一个条目,其中key = {1,3,5}和value = {@hello}。我能通过传入集合{3,5,1}从这个字典条目检索吗?

I know you can use any object as key for NSDictionary but the problem is will it be able to retrieve the correct value? Support I have an entry where the key = {1,3,5} and value = { @"hello" }. Will I be able to retrieve from this dictionary entry by passing in the set {3,5,1}?

按顺序,是基于指针匹配的键,还是它实际上比较集合的内容? (如果是前者,我该如何解决这个问题?)

In order words, is the key matched based on the pointer or does it actually compare the contents of the set? (And if it is the former, how can I overcome this?)

推荐答案

有任何陷阱)。

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

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

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