2013-05-22 70 views
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」如何在單個可變數組中組合兩個可變數組值?

我mutablearray值

testArray = AA,BB,CC,DD ... testArray1 = XX,YY ,ZZ,SS ...

我想我期待像輸出

AA XX,YY的BB,CC ZZ,DD SS

回答

1

試試這個:

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

    [newArray addObject tmpObject]; 
    tmpObject=nil; 
} 
+0

這行代碼是什麼意思: - [[testArray1 objectAtIndex:i]];' –

+0

read complete line NSString * tmpObject = [NSString stringWithFormat:@「%@%@」,[testArray objectAtIndex:i], [testArray1 objectAtIndex :一世]]; – BhushanVU

+0

喔ok了我的mistak,但我不認爲它neead一個創建newArray他們首先在添加到第二陣列就是這樣 –

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

} 

讓我知道這是工作或不!!!

+0

我得到這個錯誤「沒有可見@interface爲' NSMutableArray的」宣佈選擇‘ADDOBJECT’」 – Siva

+0

有你定義數組作爲一個的NSArray或NSMutableArray的??? .. – NiravPatel

+0

錯誤清除。但沒有工作你的代碼... – Siva

0

爲AA XX,YY的BB,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]; 
} 
0

不需要去第三陣列。您可以使用NSMutableArrayreplaceObjectAtIndex方法。

這樣..

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