NSMutablearray move object from index to index

IphoneObjective CNsarray

Iphone Problem Overview


I have a UItableview with reordable rows and the data is in an NSarray. So how do I move an object in the NSMutablearray when the appropriate tableview delegate is called?

Another way to ask this is how to reorder an NSMutableArray?

Iphone Solutions


Solution 1 - Iphone

id object = [[[self.array objectAtIndex:index] retain] autorelease];
[self.array removeObjectAtIndex:index];
[self.array insertObject:object atIndex:newIndex];

That's all. Taking care of the retain count is important, since the array might be the only one referencing the object.

Solution 2 - Iphone

ARC compliant category:

NSMutableArray+Convenience.h

@interface NSMutableArray (Convenience)

- (void)moveObjectAtIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex;

@end

NSMutableArray+Convenience.m

@implementation NSMutableArray (Convenience)

- (void)moveObjectAtIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex
{
    // Optional toIndex adjustment if you think toIndex refers to the position in the array before the move (as per Richard's comment)
    if (fromIndex < toIndex) {
        toIndex--; // Optional 
    }

    id object = [self objectAtIndex:fromIndex];
    [self removeObjectAtIndex:fromIndex];
    [self insertObject:object atIndex:toIndex];
}

@end

Usage:

[mutableArray moveObjectAtIndex:2 toIndex:5];

Solution 3 - Iphone

With Swift's Array it's as easy as this:

Swift 3
extension Array {
	mutating func move(at oldIndex: Int, to newIndex: Int) {
		self.insert(self.remove(at: oldIndex), at: newIndex)
	}
}
Swift 2
extension Array {
    mutating func moveItem(fromIndex oldIndex: Index, toIndex newIndex: Index) {
        insert(removeAtIndex(oldIndex), atIndex: newIndex)
    }
}

Solution 4 - Iphone

If you have an NSArray, you can't move or reorder anything as it is immutable.

You need an NSMutableArray. With that, you can add and replace objects which, of course, also means you can reorder the array.

Solution 5 - Iphone

You can't. NSArray is immutable. You can copy that array into an NSMutableArray (or use that in the first place). The mutable version has methods to move and exchange its items.

Solution 6 - Iphone

I guess if I understand correctly, you can do:

- (void) tableView: (UITableView*) tableView moveRowAtIndexPath: (NSIndexPath*)fromIndexPath toIndexPath: (NSIndexPath*) toIndexPath

{
    [self.yourMutableArray moveRowAtIndex: fromIndexPath.row toIndex: toIndexPath.row]; 
    //category method on NSMutableArray to handle the move
}

Then what you can do is to add a category method to NSMutableArray using – insertObject:atIndex: method to handle the move.

Solution 7 - Iphone

Similar to Tomasz but with out of range error handling

enum ArrayError: ErrorType {
    case OutOfRange
}

extension Array {
    mutating func move(fromIndex fromIndex: Int, toIndex: Int) throws {
        if toIndex >= count || toIndex < 0 {
            throw ArrayError.OutOfRange
        }
        insert(removeAtIndex(fromIndex), atIndex: toIndex)
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJonathan.View Question on Stackoverflow
Solution 1 - IphoneJoostView Answer on Stackoverflow
Solution 2 - IphoneOliver PearmainView Answer on Stackoverflow
Solution 3 - IphoneTomasz BąkView Answer on Stackoverflow
Solution 4 - IphonebbumView Answer on Stackoverflow
Solution 5 - IphoneSixten OttoView Answer on Stackoverflow
Solution 6 - IphonesaurbView Answer on Stackoverflow
Solution 7 - IphoneDavid PettigrewView Answer on Stackoverflow