Objective-C - 如何比较数组并提取差异?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1649198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:26:33  来源:igfitidea点击:

Objective-C - How to compare arrays and extract the difference?

objective-carrays

提问by marione

Possible duplicate: comparing-two-arrays

可能重复比较两个数组

I have two NSArray and I'd like to create a new Array with objects from the second array but not included in the first array.

我有两个 NSArray,我想用第二个数组中的对象创建一个新数组,但不包含在第一个数组中。

Example:

NSMutableArray *firstArray = [NSMutableArray arrayWithObjects:@"Bill", @"Ben", @"Chris", @"Melissa", nil];
NSMutableArray *secondArray = [NSMutableArray arrayWithObjects:@"Bill", @"Paul", nil];

The resulting array should be: 

[@"Paul", nil];

I solved this problem with a double loop comparing objects into the inner one.

我通过将对象与内部对象进行比较的双循环解决了这个问题。

Is there a better solutions ?

有没有更好的解决方案?

回答by Jean Regisser

[secondArray removeObjectsInArray:firstArray];

This idea was taken from another answer.

这个想法来自另一个答案

回答by teabot

If duplicate items are notsignificant in the arrays, you can use the minusSet:operation of NSMutableSet:

如果重复的项目是不是在阵列显著,你可以使用minusSet:的操作 NSMutableSet

NSMutableArray *firstArray = [NSMutableArray arrayWithObjects:@"Bill", @"Ben", @"Chris", @"Melissa", nil];
NSMutableArray *secondArray = [NSMutableArray arrayWithObjects:@"Bill", @"Paul", nil];

NSSet *firstSet = [NSSet setWithArray:firstArray];
NSMutableSet *secondSet = [NSMutableSet setWithCapacity:[secondArray count]];
[secondSet addObjectsFromArray:secondArray];

[secondSet minusSet:firstSet]; // result is in `secondSet`

回答by Anil Gupta

I want to compare images from two NSArray. One Array, I was getting from Core Database. Second I have constant array objects.

我想比较来自两个 NSArray 的图像。一个数组,我是从核心数据库中获取的。其次,我有常量数组对象。

I want to know that object of second array is present in Core database or not.

我想知道 Core 数据库中是否存在第二个数组的对象。

Here is code which i used.

这是我使用的代码。

// All object from core data and take into array.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]initWithEntityName:@"student"];

NSArray *dbresult = [[NSArray alloc]init];
NSError *error;
@try {
    dbresult = [context executeFetchRequest:fetchRequest error:&error];
}
@catch (NSException *exception) {
    NSString *logerror = [NSString stringWithFormat:@"error in fetching Rooms from coredata = %@",exception.description];
NSLog(logerror)
}
@finally {
}

/*
 Get Unused images from list
 */

NSMutableArray *usedImages = [dbresult valueForKey:@"roomImageLocalPath"];

NSMutableSet *fSet = [NSMutableSet setWithArray:usedImages];
NSMutableSet *sSet = [NSMutableSet setWithCapacity:[newImages count]];
[sSet addObjectsFromArray:newImages];
[sSet minusSet:fSet];

NSArray *unusedImages = [secondSet allObjects];
NSLog(@"unusedImages %@",unusedImages);