优雅地比较两个NSArrays中的字符串

Ale*_*lex 1 objective-c

亲爱的大家.我们有2个数组(currentCarriers和带有字符串的companyList.最终解决方案必须是数组,它从第一个数组中排除相同的字符串.贝娄是我的解决方案,但可能有两个for循环不像可可风格.也许有人可以建议一些东西更好?

for (NSString *carrier in currentCarriers) {
    for (NSString *company in  companyList)
    {
        if ([company isEqualToString:carrier]) [removedCompanies addObject:company];        }
}    

NSMutableArray *companiesForAdd = [NSMutableArray arrayWithArray:companyList];
[companiesForAdd removeObjectsInArray:removedCompanies];
Run Code Online (Sandbox Code Playgroud)

Dar*_*ust 5

将一个列表转换为可变数组,然后使用removeObjectsInArray:,如:

foo = [NSMutableArray arrayWithArray:currentCarriers];
[foo removeObjectsInArray:companyList];
// foo now contains only carriers that are not in the company list.
Run Code Online (Sandbox Code Playgroud)

编辑:

替代设置差异(但在大多数情况下由于复制/分配可能更慢):

NSMutableSet *foo = [NSMutableSet setWithArray:currentCarriers];
[foo minusSet:[NSSet setWithArray:companyList]];
Run Code Online (Sandbox Code Playgroud)

对于较大的列表,这可能会更快,但是您会丢失排序(如果有的话).