1

I am new in iOS and I am facing problem regarding to concatenate two mutable array

In first array I am getting value like this

<__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
) 

In Second array

<__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
)

I need to show array like this

12/05/2017 17:12

How can I do this? Thanks in Advance!

3 Answers 3

2

As simple as below.

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

for (int i=0;i<firstArray.count;i++) {
    [finalArray addObject:[NSString stringWithFormat:@"%@ %@", [firstArray objectAtIndex:i], [secondArray objectAtIndex:i]]];
}
Sign up to request clarification or add additional context in comments.

1 Comment

@FahimParker perfect solution and easy to use.Thank you.
1

This way with count check

    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]]];
    }

Comments

1

You can do like this also

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.