我有一个NSMutableArray
。其中有这样的对象:0、1、2。
如何将对象0
替换为对象2
的索引位置?结果我想得到一个包含对象1、2、0的数组。谢谢。
我有一个NSMutableArray
。其中有这样的对象:0、1、2。
如何将对象0
替换为对象2
的索引位置?结果我想得到一个包含对象1、2、0的数组。谢谢。
id object = [[array objectAtIndex:0] retain];
[array removeObjectAtIndex:0];
[array insertObject:object atIndex:2];
[object release];
[array addObject:[array objectAtIndex:0]];
[array removeObjectAtIndex:0];
首先获取对象的一个副本,然后将其从索引0中删除,最后将其添加到索引2。
id object = [array objectAtIndex:0];
[array removeObjectAtIndex:0];
[[array insertObject:object atIndex:2];
object
,因为objectAtIndex
返回一个已保留并自动释放的对象。 - zaph