2012-02-15 47 views
0

我想在這裏多陣列多次添加到一個NSMutableArray的 其實我加入不同的值相同的陣列,以一個NSMutable陣列 此代碼:嘗試很多陣列添加到一個NSMutableArray的

NSMutableArray* wordsArray =[[NSMutableArray alloc] init ]; 
NSMutableArray* meaningsArray=[[NSMutableArray alloc]init]; 
NSMutableArray* wordsArrayTemp=[[NSMutableArray alloc]init]; 
NSMutableArray* meaningsArrayTemp=[[NSMutableArray alloc]init ]; 
NSMutableArray* allWords =[[NSMutableArray alloc]initWithCapacity:2]; 
NSMutableArray* allMeanings=[[NSMutableArray alloc]initWithCapacity:2]; 




for(int i=0;i<2;i++) 
{ 
    int wordsCounter=0; 
    [wordsArrayTemp removeAllObjects]; 
[meaningsArrayTemp removeAllObjects]; 


    for(NSString *tmp in wordsArray) 
     { 
      NSString *meaning =[meaningsArray objectAtIndex:wordsCounter++]; 

      subtmp= [ tmp substringWithRange:NSMakeRange(0,1)]; 

      if([currentTable isEqualToString:@"arabicToEnglish"]) 
      { 
       if([subtmp isEqualToString:[arabicLetters objectAtIndex:i]]) 
       { 
        [wordsArrayTemp addObject:tmp]; 
        [meaningsArrayTemp addObject:meaning]; 
       } 
      } 
      else 
       if([subtmp isEqualToString:[englishLetters objectAtIndex:i]]) 
       { 
        [wordsArrayTemp addObject:tmp]; 
        [meaningsArrayTemp addObject:meaning]; 
       }  
     } 
    [allWords insertObject:wordsArrayTemp atIndex:i]; 
    // [allWords addObject: wordsArrayTemp]; 
    [allMeanings addObject:meaningsArrayTemp]; 
    NSLog(@"all words count%i",[[allWords objectAtIndex:i] count]); 
} 

問題: 這裏假設的行爲是在allWords數組中有2個不同的值。 但實際發生的是,2個值用最後一個索引值填充相同的值。 我的意思是[allWords objectAtIndex:0]應該有2000個對象,[allWords objectAtIndex:1]應該有3000個,但是它們都有3000個對象會發生什麼!

我在這裏錯過了什麼? 日Thnx

+0

2000和3000從哪裏來? – 2012-02-15 09:03:08

+1

,你應該用'wordsArrayTemp = [NSMutableArray array];'替換'[wordsArrayTemp removeAllObjects];'。 「意義數組」也是如此。您不想清空該數組,因爲這會清空「allWords」或「allMeanings」數組中的數組。 – 2012-02-15 09:05:07

+0

從數據庫表,我試圖按字母順序排序整個單詞2應該是26實際上 – 2012-02-15 09:06:19

回答

1

當你添加一個對象數組對象是不可複製的。你只需保存它的內存地址。

基本上你將相同的臨時數組添加到父數組中。你做了所有的數組操作到同一個數組。

也許這張展開的循環代碼會使它更清晰一點。

// create new array on a specific memory address. let's say this address is 0x01 
NSMutableArray* wordsArrayTemp=[[NSMutableArray alloc]init]; 

// first iteration of your loop 

// remove all objects from array at memory address 0x01 
[wordsArrayTemp removeAllObjects]; 

// add objects to the array at address 0x01 
[wordsArrayTemp addObject:tmp]; 

// insert array (still at address 0x01) to the parent array 
[allWords insertObject:wordsArrayTemp atIndex:i]; 
// your allWords array now looks like this: {[email protected]} 

// second iteration of your loop 

// remove all objects from array at memory address 0x01!!! (still the same array as in the first iteration) 
// since it's the same array all objects from the array at [allWords objectAtIndex:0] are removed too 
[wordsArrayTemp removeAllObjects]; 

// add objects to the array at address 0x01 
[wordsArrayTemp addObject:tmp]; 

// insert array (still at address 0x01) to the parent array 
[allWords insertObject:wordsArrayTemp atIndex:i]; 
// your allWords array now looks like this {[email protected], [email protected]} 

的解決方案是很容易的。

在for循環的開始處,而不是從數組中移除allObjects,以創建新數組。
只是

wordsArrayTemp = [NSMutableArray array]; 
meaningsArrayTemp = [NSMutableArray array]; 
+0

thnx很多matthias :) – 2012-02-18 14:19:50

0

嘗試添加simutaneously一個數組:

[[allWords array] addObject:wordsArray.array]; 

希望對大家有所幫助

0

更換

[wordsArrayTemp removeAllObjects]; 
[meaningsArrayTemp removeAllObjects]; 

試試這個: -

[allWords insertObject:[wordsArrayTemp copy] atIndex:i]; 

它應該工作。