Dan*_*elR 3 collections cocoa cocoa-touch nsarray ios
假设我有两个数组:
NSArray * first = @[@"One", @"Two", @"Three"," @Four"];
NSArray * second = @[@"Four", @"Five", @"Six", @"One"];
Run Code Online (Sandbox Code Playgroud)
我想将两者中的对象放入另一个数组中:
NSArray * both = @[@"Four", @"One"];
Run Code Online (Sandbox Code Playgroud)
有没有比通过第一个项目的每个项目更优雅的方式并检查它是否包含在第二个项目中?
你基本上需要找到数组的交集,所以你需要在这里使用set:
NSMutableSet *intersection = [NSMutableSet setWithArray:firstArray];
[intersection intersectSet:[NSSet setWithArray:secondArray]];
NSArray *resultArray = [intersection allObjects];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
426 次 |
| 最近记录: |