Move object to another index

suggest change

Move 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"].

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents