0
- (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

0

7 Answers 7

1

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;
}
Sign up to request clarification or add additional context in comments.

5 Comments

what is use of this line :- [testArray1 objectAtIndex:i]];
read complete line NSString *tmpObject=[NSString stringWithFormat:@"%@ %@",[testArray objectAtIndex:i], [testArray1 objectAtIndex:i]];
ohh ok it my mistak but i dont think that it neead a create newArray they add first in to second array that's it
thanks for close look..variety of logics available here @nitin ..see all five answers all got different approaches...and thats not exactly duplicate question. 147,721 questions for ios..some similarities will be there for questions coming in future..
there are lots of similar method for one task right..? so it doesn't mean that just adding one NSString variable making different implementation
1

you can do something like below..

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

int count = [testArray count]+[testArray1 count];
for(int i=0;i<count;i++)
{
 if(i%2==0)
    [aryFinal addobject:[testArray objectAtIndex:i]];
 else
    [aryFinal addobject:[testArray1 objectAtIndex:i]];

 }

let me know it is working or not!!!

6 Comments

i'm getting this error "no visible @interface for 'nsmutablearray' declares the selector 'addobject'"
have you define your array as a NSArray or NSMutableArray???..
Error cleared. But not working your code...
what happening? can you tell me my friend ??
sorry... your code also working
|
1
 NSMutableArray *array1 = [NSMutableArray arrayWithObjects:@"AA",@"BB",@"CC" nil];
 NSArray *array2 = [NSArray arrayWithObjects:@"XX",@"YY",@"ZZ" nil];

    for (int i=0; i<[array1 count];i++)
        [array1 replaceObjectAtIndex:i 
                withObject:[NSString stringWithFormat:@"%@ %@",
                            array1[i],
                            array2[i]]];
    NSLog(@"%@",array1);

Output:

"AA XX","BB YY","CC ZZ"

Comments

0

To simply add two arrays:

[testArray setArray: testArray1];

But if you want desired result:

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

for(int i = 0; i < (testArray.count + testArray1.count); i++)
{
 if(i%2 == 0)
    [arrFinal addobject:[testArray objectAtIndex:i]];
 else
    [arrFinal addobject:[testArray1 objectAtIndex:i]];
}

Comments

0

Do not need to go for third array. You can use replaceObjectAtIndex method of NSMutableArray.

This way..

    NSMutableArray *array1 = [NSMutableArray arrayWithObjects:@"aa",@"bb", nil];
    NSArray *array2 = [NSArray arrayWithObjects:@"xx",@"yy", nil];

    for (int i=0; i<[array1 count];i++)
        [array1 replaceObjectAtIndex:i withObject:[NSString stringWithFormat:@"%@ %@",array1[i],array2[i]]];

Comments

0

for aa xx, bb yy, cc zz, dd ss output:

int i=-1;
for(int k =0;k<[testArray1 count];++k)
{
    i=i+2;
    [testArray insertObject:[testArray1 objectAtIndex:k] atIndex:i];
}

Comments

0
NSMutableArray *array1,*array2;
    //////array1 and array2 initialise it with your values
    NSMutableArray *finalArray = [[NSMutableArray alloc]init]

    int totalcount = 0;

    if (array1.count > array2.count) {
       totalcount =  array1.count;
    }
    else
        totalcount =  array2.count;

    for (int i = 0; i<totalcount; i++) {
        if (i <= array1.count-1) {
            [finalArray addObject:[array1 objectAtIndex:i]];
        }
        if (i <= array2.count-1) {
            [finalArray addObject:[array2 objectAtIndex:i]];
        }
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.