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