7

I have the code below, which works in swift 2.3. I am struggling to understand how to convert it to swift 3/4 - the issue it those is Value of type 'Range<Int>' has no member 'map'

let grainSize = CGFloat(0.01)
        let min = CGFloat(-3)
        let max = CGFloat(3)
        let range = Range<Int>(uncheckedBounds: (lower: Int(min/grainSize), upper: Int(max/grainSize)))
        let lol = NSRange(range)

        var points = range.map { step -> CGPoint in
            let i = grainSize * CGFloat(step)
            let x = x_base + (i * controller.width / 4.0)
            let y = y_base + (m * equation(i))
            if x_init == CGFloat.max { x_init = x }
            return CGPointMake(x, y)
        }
        points.append(CGPointMake(x_init, y_base))
        points.forEach { renderer.lineTo($0) }

I am wondering if someone can point me in the right direction for this - even to documentation regarding this, as I can't find anything about it in apple docs either =[

stktrc
  • 1,599
  • 18
  • 30

1 Answers1

9

Range does not adopt Sequence, just create the range literally as CountableClosedRange

let range = Int(min/grainSize)...Int(max/grainSize)
vadian
  • 274,689
  • 30
  • 353
  • 361
  • This 100% worked. Thank you so much. Can you point me to some kind of documentation on this so I can understand a bit more about it? – stktrc Aug 21 '17 at 12:54
  • 10
    Alternatively (no range needed): `var points = stride(from: min, through: max, by: grainSize).map { i in ... }` – Martin R Aug 21 '17 at 13:26
  • @MartinR this is exactly what I was looking for! Thank you – nomnom Nov 20 '19 at 14:47