2010-11-08 67 views
0

我有一個NSMutableArray;在nsmutablearray中移動對象

NSMutableArray 
--NSMutableArray 
----NSDictionary 
----NSDictionary 
----NSDictionary 
--NSMutableArray 
----NSDictionary 
----NSDictionary 
----NSDictionary 

我想先將NSDictionary移動到第二個NSMutableArray。 這裏是代碼:

id tempObject = [[tableData objectAtIndex:fromSection] objectAtIndex:indexOriginal]; 
[[tableData objectAtIndex:fromSection] removeObjectAtIndex:indexOriginal]; 
[[tableData objectAtIndex:toSection] insertObject:tempObject atIndex:indexNew]; 

它消除了對象,但不能插入對象到新的位置。 錯誤是:

[CFDictionary retain]: message sent to deallocated instance 0x4c45110 

在頭文件:

NSMutableArray *tableData; 
@property (nonatomic, retain) NSMutableArray *tableData; 

我怎麼可以重新排列/移動對象的NSMutableArray?

回答

5

當一個對象從可變數組中移除時,它將發送release消息。因此,如果沒有別的東西持有對它的引用,該對象將被釋放。

所以,你可以簡單地重新排序聲明:

[[tableData objectAtIndex:toSection] insertObject:tempObject atIndex:indexNew]; 
[[tableData objectAtIndex:fromSection] removeObjectAtIndex:indexOriginal]; 

...或者明確保留的對象活着:閱讀

[tempObject retain]; 
[[tableData objectAtIndex:fromSection] removeObjectAtIndex:indexOriginal]; 
[[tableData objectAtIndex:toSection] insertObject:tempObject atIndex:indexNew]; 
[tempObject release]; 

通過Array FundamentalsMutable Arrays的更多細節。