我有一个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中的对象?
答案 0 :(得分: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 Fundamentals和Mutable Arrays了解详情。