Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.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
Ios 将两个数组中的所有字符串放入另一个数组_Ios_Cocoa_Cocoa Touch_Collections_Nsarray - Fatal编程技术网

Ios 将两个数组中的所有字符串放入另一个数组

Ios 将两个数组中的所有字符串放入另一个数组,ios,cocoa,cocoa-touch,collections,nsarray,Ios,Cocoa,Cocoa Touch,Collections,Nsarray,假设我有两个数组: NSArray * first = @[@"One", @"Two", @"Three"," @Four"]; NSArray * second = @[@"Four", @"Five", @"Six", @"One"]; 我想将两者中的对象放入另一个数组: NSArray * both = @[@"Four", @"One"]; 有没有比检查第一个数组中的每个项目并检查其是否包含在第二个数组中更优雅的方法?从两个数组中创建两个NSMutableSet实例。然后做: N

假设我有两个数组:

NSArray * first = @[@"One", @"Two", @"Three"," @Four"];
NSArray * second = @[@"Four", @"Five", @"Six", @"One"];
我想将两者中的对象放入另一个数组:

NSArray * both = @[@"Four", @"One"]; 

有没有比检查第一个数组中的每个项目并检查其是否包含在第二个数组中更优雅的方法?

从两个数组中创建两个
NSMutableSet
实例。然后做:

NSArray *result = [[set1 intersectSet:set2] allObjects];

当然。只需使用正确的工具执行正确的任务。别名,使用集合执行集合操作

NSSet *first = [NSSet setWithArray:array1];
NSMutableSet *second = [NSMutableSet setWithArray:array2];
[second intersectSet:first];
NSArray *commonObjects = [second allObjects];

基本上需要找到阵列的交点,因此需要在此处使用set:

NSMutableSet *intersection = [NSMutableSet setWithArray:firstArray];
[intersection intersectSet:[NSSet setWithArray:secondArray]];

NSArray *resultArray = [intersection allObjects];