How to combine two mutable array values ​​in a single mutable array?

advertisements

This question already has an answer here:

  • How would I combine two arrays in Objective-C? 2 answers
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSError *error;
    json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
    NSLog(@"json.... %@",json);

    id jsonObject = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:nil];

    NSLog(@"jsonObject=%@", jsonObject);

    NSDictionary *checkArray=[json valueForKey:@"ND"];

    NSArray *tel = [checkArray valueForKey:@"FN"];

    testArray = [[NSMutableArray alloc]init];
    testArray1 = [[NSMutableArray alloc]init];

    newsarray = [[NSMutableArray alloc]init];

    for (id photo in tel)
    {
        if (photo == [NSNull null])
        {
            NSString *test8;
            test8 = @"empty";
            [testArray addObject:test8];

        }
        else
        {
            // photo isn't null. It's an array
            NSArray *innerPhotos = photo;

            [testArray addObject:photo];

        }

    }

    NSArray *tel1 = [checkArray valueForKey:@"LN"];

    for (id photo1 in tel1)
    {
        if (photo1 == [NSNull null])
        {
            NSString *test8;
            test8 = @"empty";
            [testArray1 addObject:test8];

        }
        else
        {
            // photo isn't null. It's an array
            //NSArray *innerPhotos1 = photo1;

            [testArray1 addObject:photo1];

        }

    }

    newsarray = [NSMutableArray arrayWithArray:[testArray arrayByAddingObjectsFromArray:testArray1]];
    NSLog(@"testArray =%@",newsarray);

here i want to combine two array values "testArray" and "testArray1"

my mutablearray values are

testArray = aa, bb, cc, dd... testArray1= xx, yy, zz, ss...

i would like to expect my output like

aa xx, bb yy, cc zz, dd ss


Try this:

for (int i=0;i<[testArray count];i++){
    NSString *tmpObject=[NSString stringWithFormat:@"%@ %@",
                            [testArray objectAtIndex:i],
                            [testArray1 objectAtIndex:i]];

    [newArray addObject tmpObject];
    tmpObject=nil;
}