iOS 数组移动元素,其他元素自动移位

- (void)moveArrayItemFromIndex:(int)fromIndex toIndex:(int)toIndex{
    
    NSMutableArray *array = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"].mutableCopy;
    NSString *str = array[fromIndex];
    NSInteger currentIndex = fromIndex;
    if (fromIndex <= toIndex) {
        for (int i=fromIndex; itoIndex; i--) {
            currentIndex = [array indexOfObject:str];
            if (currentIndex > toIndex) {
                [array exchangeObjectAtIndex:currentIndex withObjectAtIndex:currentIndex - 1];
                currentIndex--;
            }
        }
    }
    
    NSLog(@"%@",array);
    
}

 

你可能感兴趣的:(ios)