如何在Objective-C中组合两个数组? [英] How would I combine two arrays in Objective-C?
本文介绍了如何在Objective-C中组合两个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
JavaScript concat()
函数的Objective-C等价物是什么?
What is the Objective-C equivalent of the JavaScript concat()
function?
假设两个对象是数组,你会如何组合它们?
Assuming that both objects are arrays, how would you combine them?
推荐答案
NSArray
's arrayByAddingObjectsFromArray:
或多或少等同于JavaScript的 .concat()
方法:
NSArray
's arrayByAddingObjectsFromArray:
is more-or-less equivalent to JavaScript's .concat()
method:
NSArray *newArray=[firstArray arrayByAddingObjectsFromArray:secondArray];
注意:如果 firstArray
为nil, newArray
将为零。这可以通过使用以下方法解决:
Note: If firstArray
is nil, newArray
will be nil. This can be fixed by using the following:
NSArray *newArray=firstArray?[firstArray arrayByAddingObjectsFromArray:secondArray]:[[NSArray alloc] initWithArray:secondArray];
如果你想删除重复项:
NSArray *uniqueEntries = (NSArray *)[[NSSet setWithArray:newArray] allObjects];
这篇关于如何在Objective-C中组合两个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文