我有2个数组。数组1返回不同的数字。例如:(29,30)。数组2有对象。例如:
({
IDCode = 5;
dateEnd = "04/08/2016";
dateStart = "04/08/2016";
},
{
IDCode = 30;
dateEnd = "04/09/2016";
dateStart = "04/09/2016";
},
{
IDCode = 55;
dateEnd = "04/10/2016";
dateStart = "04/10/2016";
})
我想将第一个数组与第二个数组的IDCode进行比较。如果它们匹配,我想制作一个只有匹配数字的新数组。如果使用上面的例子,我只会返回array3
{
IDCode = 30;
dateEnd = "04/09/2016";
dateStart = "04/09/2016";
}
我做了一个返回IDCode == {"29", "30"}
的谓词但是我没有匹配。
我想我必须创建一个循环,只测试第二个数组的valueForKey,但我不知道从哪里开始。任何帮助都会很棒!
答案 0 :(得分:0)
您必须执行以下代码行之类的操作。使用 IN 。
NSPredicate *intersectPredicate = [NSPredicate predicateWithFormat:@"ANY IDCode IN(%@), otherArray];
NSArray *result = [your_array filteredArrayUsingPredicate:intersectPredicate];
参考链接:http://ronnqvi.st/array-intersections-and-differences/
<强>更新强>
{{1}}
答案 1 :(得分:0)
很容易使用NSMutableSet
NSMutableSet *set1 = [NSMutableSet setWithArray: array1];
NSSet *set2 = [NSSet setWithArray: array2];
[set1 intersectSet: set2];
NSArray *resultArray = [set1 allObjects];
希望它会有所帮助:)
NSArray *arr = @[ @{ @"IDCode" : @"5",
@"dateEnd" : @"04/08/2016",
@"dateStart" : @"04/08/2016"} ,
@{ @"IDCode" : @"30",
@"dateEnd" : @"04/09/2016",
@"dateStart" : @"04/09/2016"},
@{ @"IDCode" : @"55",
@"dateEnd" : @"04/10/2016",
@"dateStart" : @"04/10/2016"}];
NSArray *ar2 = @[@{ @"IDCode" : @"5",
@"dateEnd" : @"04/08/2016",
@"dateStart" : @"04/08/2016"}];
NSMutableSet *set1 = [NSMutableSet setWithArray: arr];
NSSet *set2 = [NSSet setWithArray: ar2];
[set1 intersectSet: set2];
NSArray *resultArray = [set1 allObjects];
它对我有用......
答案 2 :(得分:0)
这是你的解决方案!!效率也很高。
NSArray *IDCodeArray = @[@29, @30, @31];
NSArray *dataArray = @[
@{
@"IDCode" : @5,
@"dateEnd" : @"04/08/2016",
@"dateStart" : @"04/08/2016"
},
@{
@"IDCode" : @30,
@"dateEnd" : @"04/09/2016",
@"dateStart" : @"04/09/2016"
},
@{
@"IDCode" : @55,
@"dateEnd" : @"04/10/2016",
@"dateStart" : @"04/10/2016"
},
@{
@"IDCode" : @29,
@"dateEnd" : @"04/11/2016",
@"dateStart" : @"04/11/2016"
}
];
NSMutableArray *resultArray = [[NSMutableArray alloc] init];
// loop through each object using fast enumeration
[IDCodeArray enumerateObjectsUsingBlock:^(NSNumber *idcode, NSUInteger idx, BOOL * _Nonnull stop) {
__block NSUInteger foundIndex = NSNotFound;
// loop through data array to find IDcode
foundIndex = [dataArray indexOfObjectPassingTest:^BOOL(NSDictionary *dictObject, NSUInteger idx, BOOL * _Nonnull stop) {
// stop the loop if found
NSUInteger IDCodeObject = [dictObject valueForKey:@"IDCode"];
return (IDCodeObject.integerValue == idcode.integerValue);
}];
// add object to result array if found
if(foundIndex != NSNotFound){
[resultArray addObject:[dataArray objectAtIndex:foundIndex]];
}
}];
NSLog(@"Result Array : %@", resultArray);
===================================
查找包含重复代码的所有匹配值
// loop through each object using fast enumeration
[IDCodeArray enumerateObjectsUsingBlock:^(NSNumber *idcode, NSUInteger idx, BOOL * _Nonnull stop) {
// loop through data array to find IDcode
[dataArray enumerateObjectsUsingBlock:^(NSDictionary *dictObject, NSUInteger index, BOOL * _Nonnull stop) {
NSUInteger IDCodeObject = [[dictObject valueForKey:@"IDCode"] integerValue];
if(IDCodeObject == idcode.integerValue){
[resultArray addObject:[dataArray objectAtIndex:index]];
}
}];
}];