swift convert Range<Int> to [Int]

haitham picture haitham · Aug 19, 2015 · Viewed 40.1k times · Source

how to convert Range to Array

I tried:

let min = 50
let max = 100
let intArray:[Int] = (min...max)

get error Range<Int> is not convertible to [Int]

I also tried:

let intArray:[Int] = [min...max]

and

let intArray:[Int] = (min...max) as [Int] 

they don't work either.

Answer

David Skrundz picture David Skrundz · Aug 19, 2015

You need to create an Array<Int> using the Range<Int> rather than casting it.

let intArray: [Int] = Array(min...max)