Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use range.map in swift 3/4?

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 =[

like image 736
stktrc Avatar asked Aug 21 '17 12:08

stktrc


People also ask

How does range work in Swift?

Ranges in Swift allow us to select parts of Strings, collections, and other types. They're the Swift variant of NSRange which we know from Objective-C although they're not exactly the same in usage, as I'll explain in this blog post. Ranges allow us to write elegant Swift code by making use of the range operator.

How do you create a range in Swift?

< range operators are a shorthand way of creating ranges. For example: let myRange = 1..<3. let myRange = CountableRange<Int>(uncheckedBounds: (lower: 1, upper: 3)) // 1..<3.

How does map work in Swift?

The map() Function in Swift. In Swift, you can use the built-in map() function to modify each element in a collection. The map() function loops through the collection, applying a function for each element. The map() function always returns an array where the transformed values are.

How do I map an array in Swift?

Example 2: Uppercase Array Of Strings Using map() In the above example, we have used the map() and uppercased() methods to transform each element of the languages array. The uppercased() method converts each string element of an array to uppercase. And the converted array is stored in the result variable.


1 Answers

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

let range = Int(min/grainSize)...Int(max/grainSize)
like image 54
vadian Avatar answered Oct 11 '22 09:10

vadian