Swift:如何将数组中的元素换成1?

时间:2016-09-10 16:54:16

标签: arrays swift

我有6种不同颜色的数组:

var colorList: [String] = ["blue", "green", "purple", "red", "yellow", "orange"]

我正在尝试创建一个函数,将每个元素向右移动一个,如下所示:

["orange", "blue", "green", "purple", "red", "yellow"]

我该怎么做?

1 个答案:

答案 0 :(得分:5)

$(labArr[i].selector).find(".panel-default:not(.panel-body, .panel-heading)").find("p").remove();

或作为Array

的扩展
colorList.insert(colorList.popLast()!, atIndex: 0)
extension Array {
    mutating func shiftRight() {
        if let obj = self.popLast(){
            self.insert(obj, atIndex: 0)
        }
    }
}

结果

var colorList: [String] = ["blue", "green", "purple", "red", "yellow", "orange"]
colorList.shiftRight()
print(colorList)