swift convert Range to [Int]

前端 未结 8 594
感动是毒
感动是毒 2021-02-03 16:51

how to convert Range to Array

I tried:

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

get error Range is

相关标签:
8条回答
  • 2021-02-03 17:18

    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.

    0 讨论(0)
  • 2021-02-03 17:20

    Put the Range in the init.

    let intArray = [Int](min...max)
    
    0 讨论(0)
提交回复
热议问题