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

在这里我想组合两个数组值“testArray”和“testArray1”

我的可变数组值是

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

我希望我的输出像

aa xx, bb yy, cc zz, dd ss

4

7 回答 7

1

你可以做类似下面的事情..

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

 }

让我知道它是否有效!

于 2013-05-22T06:08:54.590 回答
1

尝试这个:

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

    [newArray addObject tmpObject];
    tmpObject=nil;
}
于 2013-05-22T06:09:20.823 回答
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);

输出:

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

于 2013-05-22T07:12:51.167 回答
0

简单地添加两个数组:

[testArray setArray: testArray1];

但是如果你想要想要的结果:

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]];
}
于 2013-05-22T06:07:14.560 回答
0

对于 aa xx、bb yy、cc zz、dd ss 输出:

int i=-1;
for(int k =0;k<[testArray1 count];++k)
{
    i=i+2;
    [testArray insertObject:[testArray1 objectAtIndex:k] atIndex:i];
}
于 2013-05-22T06:15:27.917 回答
0

不需要去第三阵列。您可以replaceObjectAtIndex使用NSMutableArray.

这边走..

    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]]];
于 2013-05-22T06:19:07.540 回答
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]];
        }
    }
于 2013-05-22T06:39:17.597 回答