Replace object index in NSMutableArray

I have an NSMutableArray . For example, such objects: 0, 1, 2. How to replace object 0 with an index, where object 2 . As a result, I want an array with objects: 1, 2, 0. Thank you.

+4
source share
3 answers

@trojanfoe, there is a simple error in your answer.

The first line of code does not return anything according to the document. It should be,

 id object = [[array objectAtIndex:0] retain]; [array removeObjectAtIndex:0]; [array insertObject:object atIndex:2]; [object release]; 
+8
source
 [array addObject:[array objectAtIndex:0]]; [array removeObjectAtIndex:0]; 
0
source

First get a copy of the object, then remove it from index 0, and then add it to index 2.

 id object = [array objectAtIndex:0]; [array removeObjectAtIndex:0]; [[array insertObject:object atIndex:2]; 
0
source

Source: https://habr.com/ru/post/1382273/


All Articles