Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 比较NSMutableArray元素的值_Objective C_Nsmutablearray_Nsarray_Compare_Nsdictionary - Fatal编程技术网

Objective c 比较NSMutableArray元素的值

Objective c 比较NSMutableArray元素的值,objective-c,nsmutablearray,nsarray,compare,nsdictionary,Objective C,Nsmutablearray,Nsarray,Compare,Nsdictionary,我正在寻找一种方法来比较两个NSMutableArray对象的内容。这两个数组都填充了NSMutableDictionary,它们分别分配,但偶尔包含相同的数据 简化示例: NSMutableArray *firstArray = [[NSMutableArray alloc] init]; NSMutableArray *secondArray = [[NSMutableArray alloc] init]; NSMutableDictionary *a = [[NSDictionary a

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

简化示例:

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和c是不同的对象,但a和c的内容是匹配的

我要寻找的是一种比较firstArray和secondArray并只返回b的函数/方法

在伪代码中:

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

你是说
secondArray
中也有
a
b
?因为现在它只是空的,我不知道为什么第二个元素是特殊的。谢谢你指出这一点
secondArray
应包含
c
。修复了代码示例以反映这一点。