Can NSDictionary take a key in an NSSet?

I know that you can use any object as a key for an NSDictionary, but the problem is, can it get the correct value? Support I have an entry in which key = {1,3,5} and value = {@ "hello"}. Can I extract a record from this dictionary by going to the {3,5,1} set?

In word order, is the key mapped based on a pointer or does it really compare the contents of the set? (And if this is the first, how can I overcome this?)

+3
source share
4 answers

Yes, it looks like it is working well (not sure if there are any errors).

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
+3
source

isEqual . , {1,3,5} {3,5,1} ( , NSNUmber) YES.

+6

( NSCopying isEqual:) : . , .

+1

.

irb:

require 'osx/cocoa'
abc=OSX::NSSet.setWithArray([1,2,3])
cba=OSX::NSSet.setWithArray([3,2,1])
dict=OSX::NSMutableDictionary.dictionary
dict[abc] = 'hello'
puts dict[cba]

( , isEqual: NSSet , , , NSDictionary )

0

Source: https://habr.com/ru/post/1724995/


All Articles