如何在Objective C中连接两个可变数组

时间:2017-05-09 06:19:12

标签: objective-c arrays xcode

我是iOS的新手,我在连接两个可变数组

方面遇到了问题

在第一个数组中,我得到了这样的价值

<__NSArrayI 0x7f9102d7a100>(
12/05/2017,
17/05/2017,
17/05/2017,
17/05/2017,
17/05/2017,
22/05/2017,
22/05/2017,
22/05/2017,
22/05/2017,
22/05/2017,
22/05/2017,
22/05/2017,
22/05/2017,
22/05/2017,
22/05/2017,
22/05/2017,
23/05/2017,
23/05/2017
) 

在第二个数组中

<__NSArrayI 0x7f9102d7a360>(
17:12,
14:11,
14:25,
17:07,
18:11,
10:04,
10:05,
10:07,
10:53,
13:05,
16:01,
16:31,
16:38,
17:40,
17:44,
17:47,
09:38,
11:25
)

我需要像这样显示数组

12/05/2017 17:12

我该怎么做? 在此先感谢!

3 个答案:

答案 0 :(得分:2)

简单如下。

NSMutableArray *finalArray = [[NSMutableArray alloc] init];

for (int i=0;i<firstArray.count;i++) {
    [finalArray addObject:[NSString stringWithFormat:@"%@ %@", [firstArray objectAtIndex:i], [secondArray objectAtIndex:i]]];
}

答案 1 :(得分:1)

这种方式有计数检查

    NSArray *arr1 = @[@"12/05/2017",
                     @"17/05/2017"];
    NSArray *arr2 = @[@"17:12",
                      @"14:11"];
    NSMutableArray *result = [NSMutableArray new];

    NSAssert(arr1.count == arr2.count, @"arrays count not equal");

    for (int i = 0; i < arr1.count; i++) {
        [result addObject:[NSString stringWithFormat:@"%@ %@", arr1[i], arr2[i]]];
    }

答案 2 :(得分:1)

你也可以这样做

NSMutableArray *array1 = [@[@"111",@"222"] mutableCopy]; // sample array
NSMutableArray *array2 = [@[@"333",@"444"] mutableCopy]; // sample array
NSMutableArray *array3 = [NSMutableArray arrayWithArray:array1];
[array3 addObjectsFromArray:array2];