157

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.

haitham
  • 3,398
  • 6
  • 21
  • 35

8 Answers8

311

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

let intArray: [Int] = Array(min...max)
David Skrundz
  • 13,067
  • 6
  • 42
  • 66
46

Put the Range in the init.

let intArray = [Int](min...max)
Mr Beardsley
  • 3,743
  • 22
  • 28
18

I figured it out:

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

Giving credit to someone else.

haitham
  • 3,398
  • 6
  • 21
  • 35
15

Use map

let min = 50
let max = 100
let intArray = (min...max).map{$0}
vadian
  • 274,689
  • 30
  • 353
  • 361
  • adding why it works would be beneficial for people who are reading this kind of questions. For example stating that it creates a CoutableClosedRange which implements Sequence protocol, therefore map is possible and it yields a new int lazily when performing map operation. Some background knowledge is always good – denis631 Dec 05 '17 at 12:15
  • The other answers are interesting but this one actually answers the question. Big up. – Dan Rosenstark Jan 12 '19 at 21:05
15

do:

let intArray = Array(min...max)

This should work because Array has an initializer taking a SequenceType and Range conforms to SequenceType.

ad121
  • 2,268
  • 1
  • 18
  • 24
4

Interesting that you cannot (at least, with Swift 3 and Xcode 8) use Range<Int> object directly:

let range: Range<Int> = 1...10
let array: [Int] = Array(range)  // Error: "doesn't conform to expected type 'Sequence'"

Therefore, as it was mentioned earlier, you need to manually "unwrap" you range like:

let array: [Int] = Array(range.lowerBound...range.upperBound)

I.e., you can use literal only.

devforfu
  • 1,570
  • 1
  • 19
  • 41
  • The reason for this is because Ranges aren't Collections. The distinction between Range and Countable Range is that the latter can be iterated over by an Integer value. For that reason Countable Range is a collection type in swift but range is not. – Corey Zambonie Nov 15 '17 at 16:04
  • That's right, and I wasn't expecting this, i.e. two different range classes. Probably due to Pythonic interpretation of `range()`. – devforfu Nov 15 '17 at 16:13
3

Since Swift 3/Xcode 8 there is a CountableRange type, which can be handy:

let range: CountableRange<Int> = -10..<10
let array = Array(range)
print(array)
// prints: 
// [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

It can be used directly in for-in loops:

for i in range {
    print(i)
}
killobatt
  • 41
  • 2
1

You can implement ClosedRange & Range instance intervals with reduce() in functions like this.

func sumClosedRange(_ n: ClosedRange<Int>) -> Int {
    return n.reduce(0, +)
}
sumClosedRange(1...10) // 55


func sumRange(_ n: Range<Int>) -> Int {
    return n.reduce(0, +)
}
sumRange(1..<11) // 55
Edison
  • 11,881
  • 5
  • 42
  • 50