2011-12-27 65 views
0

我有一個數組列表,其中的每個元素都是數組。我知道,這有點怪異,但它是必要的。所以,我需要替換最深的數組元素。我試過這個:Array-within-array項目替換

for (int i = 0; i < [myArray count]; i++) { 
    for (int j = 0; j < [[myArray objectAtIndex:i] count]; j++) { 
     for (int k = 0; k < [[[myArray objectAtIndex:i] objectAtIndex:j] count]; k++) { 
      [[[myArray objectAtIndex:i] objectAtIndex:j] replaceObjectAtIndex:k withObject:@"Some_string"]; 
     } 
    } 
} 

並且得到了一個錯誤*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance。但我可以記錄這個元素,例如NSLog(@"%@", [NSString stringWithFormat:@"%@",[[[myArray objectAtIndex:0] objectAtIndex:0]objectAtIndex:0]]);,沒關係。

它可能是什麼?謝謝你的幫助。

回答

1

您的陣列實例是NSMutableArray而不是不可變的NSArray

+0

是的。我應該將其轉換爲NSMutableArray?更新:我通過'NSMutableArray * myMutArray = [myArray mutableCopy]'從myArray創建可變數組,但它沒有幫助。 – Akki 2011-12-27 15:56:06

+0

你能表達一下自己嗎? – LordTwaroog 2011-12-27 15:57:16

+2

我認爲問題是你需要內部數組可變。因此,不是[myArray mutableCopy]執行'myMutArray = [NSMutableArray array]',然後通過myArray併爲每個子數組添加它的mutableCopy到新的'myMutArray'。 – mrueg 2011-12-27 16:06:30