我有一個問題:我有一個NSMutableArray,我在多個視圖中傳遞。在其中一個視圖中,可以編輯該數組(NSMutableDictionary)內的對象的值。我這樣做使用代碼:編輯數組中的NSMutableDictionary
NSMutableDictionary *tester = [[NSMutableDictionary alloc] initWithDictionary:selectedItem copyItems:YES];
NSMutableString *newLoc = [[NSMutableString alloc] initWithString:locationField.text];
[tester setValue:newLoc forKey:@"Location"];
[selectedList replaceObjectAtIndex:[selectedList indexOfObject:selectedItem] withObject:tester];
我遇到的問題是取代selectedList中的對象。它給出了錯誤*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance 0xa631e30'
它的工作原理是,如果我將新項目複製到selectedList的複製數組中,並再次使用新值分配selectedList,但其他視圖在數組中找到相同的列表時遇到問題(新分配的位置)。
TL; DR版本:如何在NSMutableArray中編輯值(通過替換?)?爲什麼不replaceObjectAtIndex工作?
編輯:它確實是不可改變的。不過,主要問題仍然是: 我有:
NSMutableDictionary *tester = [[NSMutableDictionary alloc] initWithDictionary:geselecteerdItem copyItems:YES];
[geselecteerdItem setValue:nieuweLoc forKey:@"Location"];
[geselecteerdeLijst replaceObjectAtIndex:[geselecteerdeLijst indexOfObject:tester] withObject:geselecteerdItem];
當我使用:[warningList replaceObjectAtIndex:[warningList indexOfObject:geselecteerdeLijst] withObject:keuzeLijst]
,它給了我一個outofbounds錯誤,因爲陣列geselecteerdeLijst的索引中的warningList明顯變化。有任何想法嗎?
selectedItem你的字典,如果是的話那麼你的數組selectedList是否包含字典。請詳細說明您的數據結構。您可以將nslog放入以查看值是否正確。 – Gypsa 2011-04-26 08:11:47