Objective c 比较NSMutableArray元素的值
我正在寻找一种方法来比较两个NSMutableArray对象的内容。这两个数组都填充了NSMutableDictionary,它们分别分配,但偶尔包含相同的数据 简化示例: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 *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
。修复了代码示例以反映这一点。