0

I have Two NSMutableArray Array I want to combine this array in to old one array.

How can i do this without any loop (for, while).

         NSMutableArray *oldArray = [[NSMutableArray alloc]init];
         [oldArray addObject:@"four"];
         [oldArray addObject:@"five"];
         [oldArray addObject:@"six"];

         NSMutableArray *newArray = [[NSMutableArray alloc]init];
         [newArray addObject:@"one"];
         [newArray addObject:@"two"];
         [newArray addObject:@"three"];

After Combine I want output like this :

         NSLog(@"After Combine : %@",oldArray);
         //Output : one, two, three, four, five, six
0

3 Answers 3

1

Insert newArray in oldArray:

[oldArray insertObjects:newArray atIndexes:[[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(0, newArray.count)]]
Sign up to request clarification or add additional context in comments.

1 Comment

Its Working Bro. Thanks @willeke
0

You can use addObjectesFromArray to get results

 [oldArray addObjectsFromArray: newArray];

2 Comments

addObjectsFromArray adds the objects in oldArray to newArray and returns void.
Now the order is four, five, six, one, two, three.
0

This should do it.

newArray = [[newArray arrayByAddingObjectsFromArray:oldArray] mutableCopy];

for (NSString *str in newArray) {
    NSLog(@"%@", str);
}

output: [one,two,three,four,five,six]

wait, thats was copying into the new one.. :)

 oldArray = [[newArray arrayByAddingObjectsFromArray:oldArray] mutableCopy];

output: [one,two,three,four,five,six]

hahaha, possibly with the side effect that newArray is NSArray and has same content.
Check? Oh. newArray is still [one,two,three].

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.