Move object to another index
suggest changeMove Blue to the beginning of the array:
NSMutableArray *myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
NSUInteger fromIndex = 2;
NSUInteger toIndex = 0;
// retain/autorelease keeps `blue` alive across the removal under manual reference counting.
id blue = [[myColors[fromIndex] retain] autorelease];
[myColors removeObjectAtIndex:fromIndex];
[myColors insertObject:blue atIndex:toIndex];
myColors is now [@"Blue", @"Red", @"Green", @"Yellow"].
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents