How to create an array of colors that repeats dynamically?

I have an array of six colors and another array of 8 elements. Is there a way to repeat colorsArray as the number of elements starting from the first color in colorsArray in the same order?

var colorsArray = [UIColor.redColor(), UIColor.purpleColor(), UIColor.blueColor(), UIColor.greenColor(), UIColor.yellowColor(), UIColor.orangeColor()];

the new array will look like this:

newColorsArrayByItemsNumber = [UIColor.redColor(), UIColor.purpleColor(), UIColor.blueColor(), UIColor.greenColor(), UIColor.yellowColor(), UIColor.orangeColor(), UIColor.redColor(), UIColor.purpleColor()];
+4
source share
5 answers

You should just add the elements in the correct order to your new array

var colorsArray = [UIColor.redColor(), UIColor.purpleColor(), UIColor.blueColor(), UIColor.greenColor(), UIColor.yellowColor(), UIColor.orangeColor()]
var newColorsArrayByItemsNumber:[AnyObject] = []
let itemCount = 8
for(var i=0;i<itemCount;i++) {
    newColorsArrayByItemsNumber.append(colorsArray[i % colorsArray.count])
}
print(newColorsArrayByItemsNumber)
+2
source

You can easily repeat colorArray:

colorsArray = colorsArray + colorsArray

Then you can limit the result to 8 elements:

colorsArray = Array(colorsArray[0...7])

Combining all this in one separate line of code:

colorsArray = Array(colorsArray + colorsArray[0...1])
+1
source

, . , numberOfElements > count .

let colorsArray = [UIColor.redColor(), UIColor.purpleColor(), UIColor.blueColor(), UIColor.greenColor(), UIColor.yellowColor(), UIColor.orangeColor()]

extension Array {
    func replicateUpTo(numberOfElements: UInt) -> [Element] {
        if Int(numberOfElements) < count {
            return Array(self[0..<Int(numberOfElements)])
        } else {
            var result = self
            (0..<(Int(numberOfElements)/result.count)-1).forEach({ _ in result += self})
            return result + self[0..<(Int(numberOfElements) - (Int(numberOfElements)/count) * count)]
        }
    }
}

colorsArray.replicateUpTo(8)  // "[r 1,0 g 0,0 b 0,0 a 1,0, r 0,5 g 0,0 b 0,5 a 1,0, r 0,0 g 0,0 b 1,0 a 1,0, r 0,0 g 1,0 b 0,0 a 1,0, r 1,0 g 1,0 b 0,0 a 1,0, r 1,0 g 0,5 b 0,0 a 1,0, r 1,0 g 0,0 b 0,0 a 1,0, r 0,5 g 0,0 b 0,5 a 1,0]"
+1
source

Maybe there is another better option, but this may help you:

var i = 0
repeat{
  newColorsArrayByItemsNumber.append(colorsArray[i % colorsArray.count])
  i++
}while(newColorsArrayByItemsNumber.count<8)

print(newColorsArrayByItemsNumber)
0
source

I use the extension because this pattern keeps coming back every time I need to repeat a few repetitions:

extension Array {
    init?(count: Int, repeatedValues: [Element]) {
        var arr = [Element]()
        for _ in 0...(count/repeatedValues.count) {
            arr += repeatedValues
        }
        self = Array(arr[0..<count])
    }
}

let colorsArray = [UIColor.redColor(), UIColor.purpleColor(), UIColor.blueColor(), UIColor.greenColor(), UIColor.yellowColor(), UIColor.orangeColor()]

var newColorsArray = [UIColor](count: 8, repeatedValues: colorsArray)
0
source

All Articles