julian
julian

Reputation: 157

Combine two arrays cocoa obj-c

Given: ArrayOne = John, Bill, James, Adam

Given: ArrayTwo = Smith, Jones, Windsor, Newton

Result: ArrayThree = John Smith, Bill Jones, James Windsor, Adam Newton

Can't for the life of me figure it out. I can add ArrayTwo to the end of ArrayOne but that's not what I need.

Upvotes: 0

Views: 60

Answers (1)

i_am_jorf
i_am_jorf

Reputation: 54600

Assuming those are NSStrings in your two arrays, just iterate through and create the combined string and add to a third array:

assert(arrayOne.count == arrayTwo.count);

NSMutableArray *result = [NSMutableArray arrayWithCapacity:arrayOne.count];

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

Upvotes: 2

Related Questions