Slightly Different Implementation of moveItemAtIndex


#1

I’m at the end of chapter 10 working on providing the functionality to move rows. I made a single change to the code the book said to implement and was wondering what everyone’s opinion is about it. Instead of calling [allItems removeObjectAtIndex:from], I called the class instance method “removeItem”. Also, looking at this code I can see a use to have three more instance methods: one to retrieve a specific BNRItem, one to insert a BNRItem, and one to insert a BNRItem at a given index.

- (void) moveItemAtIndex:(int)from toIndex:(int)to
{
    if (from == to) {
        return;
    }
    
    //Get pointer to object being moved so we can re-insert it
    BNRItem *itemBeingMoved = [allItems objectAtIndex:from];
    
    //Remove the item being moved from the array
    [self removeItem:itemBeingMoved];
    
    //Insert the item being moved at the new location
    [allItems insertObject:itemBeingMoved atIndex:to];
}

Thanks.