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

标签 ios cocoa cocoa-touch collections nsarray

<分区>

假设我有两个数组:

NSArray * first = @[@"One", @"Two", @"Three"," @Four"];
NSArray * second = @[@"Four", @"Five", @"Six", @"One"];

我想将两者中的对象放入另一个数组中:

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

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

最佳答案

您基本上需要找到数组的交集,因此您需要在此处使用 set:

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

NSArray *resultArray = [intersection allObjects];

关于ios - 将两个数组中的所有字符串放入另一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17276331/

相关文章:

cocoa - 如何创建一个看起来像 Tweetie Mac OS X 应用程序新帖子窗口的窗口

cocoa - 是否可以在 NSPanel 上打开和关闭 NSNonactivatingPanelMask

iphone - NSDate 中的 +0100 值意味着什么?

ios - 使用 Objective-C/C 查找 DST(夏令时)时间戳?

ios - 在没有 IBOutlet 的情况下将 IBAction 与 UIButton 一起使用

objective-c - 尝试自动调整大小的 UITableViewCell 会导致未知错误

ios - 重新加载 UITableView 会重置 View 的 subview

macos - 操作系统: get size on disk for a file/directory

android - Cordova /Ionic2 |如何锁定和解锁Android和iOS的屏幕?

ios - 如何在 Swift 中使用查询语句检索数据?