I have this line currently, what do I need to change so I can have multiple colours?
let randomColor = arc4random_uniform(2) == 0 ? UIColor.green : UIColor.red
Thanks
I have this line currently, what do I need to change so I can have multiple colours?
let randomColor = arc4random_uniform(2) == 0 ? UIColor.green : UIColor.red
Thanks
The parameter to arc4random_uniform is the upper limit of the random values it returns (and it returns values uniformly distributed in the range 0...(upperLimit-1)). If you have (say) an array of the various colors you want to choose from, you can write:
let randomColor = colorsArray [arc4random_uniform (colorsArray.count)]
Or you could generate directly the color components randomly
let randomRed = arc4random_uniform(1)
let randomBlue = arc4random_uniform(1)
let randomGreen = arc4random_uniform(1)
let randomColor = UIColor(red: randomRed, green: randomBlue, blue: randomGreen, alpha: 1.0)
So I made an array called colours array which i filled with UiColours but when adding your line it says 'Cannot convert the value of type "Int' to expected argument type 'UInt32'"
You should be able to solve that from the error message. I didn't think about it, but arc4random_uniform takes a UInt32 parameter and returns a UInt32 result, but array indexes and counts are Int. So you need something like this:
let colorIndex = Int (arc4random_uniform (UInt32 (colorsArray.count)))
let randomColor = colorsArray [colorIndex]Or, you can write it all in one line if you want, but it starts to get ugly:
let randomColor = colorsArray [Int (arc4random_uniform (UInt32 (colorsArray.count)))]Note that there's no problem, in this case, converting back and forth from Int to UInt32, since your numbers are all small-ish and non-negative.
Works Great, Thanks