iOS 数组元素依次往后移动

2019-03-13  本文已影响0人  关灯侠

背景:[1,2,3,4],移动3到首页,得到 [3,1,2,4]效果

NSMutableArray *arr = [@[ @1, @2, @3, @4]  mutableCopy];
for (NSNumber *tmpNum in arr) {
       NSInteger currentIndex = [arr indexOfObject:@3];
       while (currentIndex - 1 >= 0) {  
          [arr exchangeObjectAtIndex:currentIndex withObjectAtIndex:currentIndex - 1];        
          currentIndex--;
      }
}
上一篇 下一篇

猜你喜欢

热点阅读