2013-11-28 33 views
0

如何添加NSMutableArray爲另一個NSMutableArray如何NSMutableArray的添加爲objectAtIndex爲NSMutableArray的

我的代碼的對象(i)爲:

yearImages = [[NSMutableArray alloc]init]; 
tempImages = [[NSMutableArray alloc]init]; 

for(int i =0; i< [yearImagesName count]; i++) 
{ 
    for(int j=0; j<[totalImagesName count]; j++) 
    { 
     if ([[totalImagesName objectAtIndex:j] rangeOfString:[yearImagesName objectAtIndex:i]].location != NSNotFound) 
     { 
      [tempImages addObject:[totalImagesName objectAtIndex:j]]; 
     } 

    } 

    [yearImages addObject:tempImages]; 
    [tempImages removeAllObjects]; 
} 

NSLog(@"\n\n year%@",[yearImages objectAtIndex:0]); // getting null result 
  • 在這裏,我需要添加tempImages爲對象(i)爲年圖像。

  • 我需要的結果如下:


[yearImages objectAtIndex:i];// result need as arrayobjects here 
+0

ü得到什麼誤差R?發佈實際的編譯器錯誤... –

+0

嘗試更改[yearImages insertObject:tempImages atIndex:i];到[yearImages addObject:tempImages]; – chancyWu

+0

@Chancy:這兩者都是相同的。 –

回答

1

添加它後,您將從tempImages中刪除對象,因此結果將是一個空數組數組。 (如果你需要或mutableCopy) [yearImages addObject:tempImages.copy]

+0

完整的臨時數據真棒...非常感謝 – svmrajesh

1

這是否甚至編譯? k in [yearImages insertObject:tempImages atIndex:k]根本沒有聲明。

你有什麼錯誤?

爲了簡化您的代碼,您可以使用此代碼擺脫索引。

yearImages = [[NSMutableArray alloc]init]; 
tempImages = [[NSMutableArray alloc]init]; 

for(NSString *yearImageName in yearImagesName) 
{ 
    for(NSString *totalImageName in totalImagesName) 
    { 
     if ([totalImageName rangeOfString:yearImageName].location != NSNotFound) 
     { 
      [tempImages addObject:totalImageName]; 
     } 
    } 

    [yearImages addObject:tempImages]; 
    [tempImages removeAllObjects]; 
} 
+0

我剛纔提到我只有沒有..現在我糾正它 – svmrajesh

+0

我得到[yearImages objectAtIndex:i] as null .. – svmrajesh

+0

您能否將完整的錯誤添加到您的問題? – tilo

1

它非常簡單:你應該添加tempImages的複印件。

更換[yearImages objectAtIndex:i][yearImages addObject:tempImages.copy]

現在看到全碼:

for(int i =0; i< [yearImagesName count]; i++) 
{ 
    for(int j=0; j<[totalImagesName count]; j++) 
    { 
     if (// Your Conditon) 
     { 
      [tempImages addObject:[totalImagesName objectAtIndex:j]]; 
     } 

    } 

    [yearImages addObject:tempImages.copy]; // each array stored as an object 
    [tempImages removeAllObjects]; 
} 
NSLog(@"\n\n year%@",[yearImages objectAtIndex:0]);