结合多个NSArrays [英] Combining multiple NSArrays

查看:106
本文介绍了结合多个NSArrays的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将多个NSArray组合成一个具有交替值的数组?例如。

How can I combine multiple NSArrays into one array with alternating values? For example.

数组一:橙色,苹果,梨

Array One: Orange, Apple, Pear

数组二:树,灌木,花

数组三:蓝色,绿色,黄色

Array Three: Blue, Green, Yellow

最终数组必须是:
Orange ,树,蓝色,苹果,灌木,绿色等

The final array would need to be: Orange, Tree, Blue, Apple, Shrub, Green, etc

推荐答案

假设数组的长度都相同:

Assuming the arrays are all of the same length:

NSUInteger numberOfArrays = 3;
NSUInteger arrayLength = [arrayOne length];
NSMutableArray *finalMutableArray = [NSMutableArray arrayWithCapacity:(arrayLength * numberOfArrays)];
for (NSUInteger index = 0; index < arrayLength; index++) {
    [finalMutableArray addObject:[arrayOne objectAtIndex:index]];
    [finalMutableArray addObject:[arrayTwo objectAtIndex:index]];
    [finalMutableArray addObject:[arrayThree objectAtIndex:index]];
}
NSArray *finalArray = [NSArray arrayWithArray:finalMutableArray];

您可能希望测试数组的长度是否相同。您不能将 nil 添加到 NSMutableArray NSArray 。您可以添加 NSNull 占位符,但最好检查输入。

You will probably want to test that the arrays are of the same length. You cannot add nil to an NSMutableArray or NSArray. You can add an NSNull placeholder, but it's probably better to check your input.

这篇关于结合多个NSArrays的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆