比较值的NSMutableArray元素 [英] Comparing NSMutableArray Elements for Values

查看:224
本文介绍了比较值的NSMutableArray元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来比较两个NSMutableArray对象的内容。这两个数组都用单独分配的NSMutableDictionaries填充,但偶尔包含相同的数据。



简化示例:

  NSMutableArray * firstArray = [[NSMutableArray alloc] init]; 
NSMutableArray * secondArray = [[NSMutableArray alloc] init];

NSMutableDictionary * a = [[NSDictionary alloc] init];
[a setObject:@fooforKey:name];
[a setObject:[NSNumber numberWithInt:1] forKey:number];

NSMutableDictionary * b = [[NSDictionary alloc] init];
[b setObject:@barforKey:name];
[b setObject:[NSNumber numberWithInt:2] forKey:number];

NSMutableDictionary * c = [[NSDictionary alloc] init];
[c setObject:@fooforKey:name];
[c setObject:[NSNumber numberWithInt:1] forKey:number];

[firstArray addObject:a];
[firstArray addObject:b];
[secondArray addObject:c];

a,b和c是不同的对象,但a和c的内容匹配。 >

我正在寻找的是一个函数/方法来比较firstArray和secondArray,只返回b。



在Pseudocode中:

  NSArray * difference = [self magicFunctionWithArray:firstArray andArray:secondArray]; 
NSLog(@%@,difference)

=> ({name =bar; number = 2})

>

解决方案

这可以通过使用 NSMutableSet 实例来实现。

  NSMutableSet * firstSet = [NSMutableSet setWithArray:firstArray]; 
NSMutableSet * secondSet = [NSMutableSet setWithArray:firstArray];

[firstSet unionSet:[NSSet setWithArray:secondArray]];
[secondSet intersectSet:[NSSet setWithArray:secondArray]];

[firstSet minusSet:secondSet];

NSLog(@%@,firstSet);


I am looking for a way to compare the contents of two NSMutableArray objects. Both arrays are filled with NSMutableDictionaries which were allocated separately but occasionally contain the same data.

Simplified Example:

NSMutableArray *firstArray = [[NSMutableArray alloc] init];
NSMutableArray *secondArray = [[NSMutableArray alloc] init];

NSMutableDictionary *a = [[NSDictionary alloc] init];
[a setObject:@"foo" forKey:"name"];
[a setObject:[NSNumber numberWithInt:1] forKey:"number"];

NSMutableDictionary *b = [[NSDictionary alloc] init];
[b setObject:@"bar" forKey:"name"];
[b setObject:[NSNumber numberWithInt:2] forKey:"number"];

NSMutableDictionary *c = [[NSDictionary alloc] init];
[c setObject:@"foo" forKey:"name"];
[c setObject:[NSNumber numberWithInt:1] forKey:"number"];

[firstArray addObject:a];
[firstArray addObject:b];
[secondArray addObject:c];

a, b and c are distinct object, but the contents of a and c match.

What I am looking for is a function / approach to compare firstArray and secondArray and return only b.

In Pseudocode:

NSArray *difference = [self magicFunctionWithArray:firstArray andArray:secondArray];
NSLog(@"%@",difference)

=> ({name="bar"; number=2})

Thank you in advance.

解决方案

This can be achieved using NSMutableSet instances.

NSMutableSet * firstSet = [NSMutableSet setWithArray:firstArray];
NSMutableSet * secondSet = [NSMutableSet setWithArray:firstArray];

[firstSet unionSet:[NSSet setWithArray:secondArray]];
[secondSet intersectSet:[NSSet setWithArray:secondArray]];

[firstSet minusSet:secondSet];

NSLog(@"%@", firstSet);

这篇关于比较值的NSMutableArray元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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