获取两个 NSDictionary 项的互斥键

发布于 2024-10-20 20:49:11 字数 179 浏览 3 评论 0原文

我有两个不同的 NSDictionary 对象,其中有属于两个集合的键,而有些键仅出现在第一个字典中,类似地,在第二个字典中找不到几个键,但在第一个字典中却找不到。

有没有一种有效的方法来进行集合比较,以提取第一个字典中存在但第二个字典中不存在的键?

基本上,在标准维恩图中,集合 A 的元素与集合 B 不相交。

I have two different 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

掩于岁月 2024-10-27 20:49:11

NSSet 是您正在寻找的:

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

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);
羁客 2024-10-27 20:49:11
NSCountedSet *dict1keys = [[NSCountedSet alloc] initWithArray:[dictionary1 allKeys]];
NSSet *dict2keys = [NSSet setWithArray:[dictionary2 allKeys]];
[dict1keys minusSet:dict2keys];
NSLog(@"Result : %@", dict1keys);
NSCountedSet *dict1keys = [[NSCountedSet alloc] initWithArray:[dictionary1 allKeys]];
NSSet *dict2keys = [NSSet setWithArray:[dictionary2 allKeys]];
[dict1keys minusSet:dict2keys];
NSLog(@"Result : %@", dict1keys);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文