2010-11-08 70 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的更多细节。