美文网首页
iOS 数组元素依次往后移动

iOS 数组元素依次往后移动

作者: 关灯侠 | 来源:发表于2019-03-13 15:14 被阅读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--;
      }
}

相关文章

网友评论

      本文标题:iOS 数组元素依次往后移动

      本文链接:https://www.haomeiwen.com/subject/zsxtmqtx.html