Getting mutually exclusive keys of two NSDictionary items
I have two d开发者_JAVA技巧ifferent NSDictionary objects where there are keys that belong to both collections while there are some keys present only in the 1st dictionary and similarly, few keys are found in the 2nd dictionary but not in the 1st.
Is there an efficient way to do a set comparison to extract the keys present in the 1st dictionary that do not exist in the 2nd?
Basically, in the standard Venn diagram, the elements of Set A that do not intersect with Set B.
NSSet
is what you're looking for:
NSMutableSet *keysInA = [NSMutableSet setWithArray:[dictionaryA allKeys]];
NSSet *keysInB = [NSSet setWithArray:[dictionaryB allKeys]];
[keysInA minusSet:keysInB];
NSLog(@"keys in A that are not in B: %@", keysInA);
NSCountedSet *dict1keys = [[NSCountedSet alloc] initWithArray:[dictionary1 allKeys]];
NSSet *dict2keys = [NSSet setWithArray:[dictionary2 allKeys]];
[dict1keys minusSet:dict2keys];
NSLog(@"Result : %@", dict1keys);
精彩评论