kod*_*dcu 0 iphone objective-c nsmutablearray
我有一个NSMutableArray;
NSMutableArray
--NSMutableArray
----NSDictionary
----NSDictionary
----NSDictionary
--NSMutableArray
----NSDictionary
----NSDictionary
----NSDictionary
Run Code Online (Sandbox Code Playgroud)
我想将第一个NSDictionary移动到第二个NSMutableArray.这是代码:
id tempObject = [[tableData objectAtIndex:fromSection] objectAtIndex:indexOriginal];
[[tableData objectAtIndex:fromSection] removeObjectAtIndex:indexOriginal];
[[tableData objectAtIndex:toSection] insertObject:tempObject atIndex:indexNew];
Run Code Online (Sandbox Code Playgroud)
它删除了对象,但无法将对象插入新位置.错误是:
[CFDictionary retain]: message sent to deallocated instance 0x4c45110
Run Code Online (Sandbox Code Playgroud)
在头文件中:
NSMutableArray *tableData;
@property (nonatomic, retain) NSMutableArray *tableData;
Run Code Online (Sandbox Code Playgroud)
我如何重新排序/移动nsmutablearray中的对象?
从可变数组中删除对象时,它会发送一条release消息.因此,如果没有其他内容保存对它的引用,则该对象将被释放.
所以你可以简单地重新排序语句:
[[tableData objectAtIndex:toSection] insertObject:tempObject atIndex:indexNew];
[[tableData objectAtIndex:fromSection] removeObjectAtIndex:indexOriginal];
Run Code Online (Sandbox Code Playgroud)
...或明确保持对象存活:
[tempObject retain];
[[tableData objectAtIndex:fromSection] removeObjectAtIndex:indexOriginal];
[[tableData objectAtIndex:toSection] insertObject:tempObject atIndex:indexNew];
[tempObject release];
Run Code Online (Sandbox Code Playgroud)
阅读Array Fundamentals和Mutable Arrays以获取更多详细信息.