stktrc
stktrc

Reputation: 1639

Convert Range<Int> to swift 3

I am trying to convert this code block to work in swift 3.

let grainSize = CGFloat(0.01)
    let min = CGFloat(-3)
    let max = CGFloat(3)
    let range = Range<Int>(start: Int(min/grainSize), end: Int(max/grainSize))

The issue I am facing is with the Range - I have tried to look through apple docs, but don't really know what I'm even looking for. The issue that it reports is that it says that Cannot convert value of type 'Int' to expected argument type 'NSRange' (aka '_NSRange')

Can anyone help help me understand what is going on here and where/what documentation I can read to make sense of this?

Upvotes: 1

Views: 588

Answers (2)

pacification
pacification

Reputation: 6018

You can get more powerful Countable range with more simple syntax:

let range = (Int(min/grainSize)..<Int(max/grainSize))

It returns CountableRange, not just Range, so you can access to RandomAccessCollection methods, for example.

And if you want to get only Range value you can cast it like this:

let range = Range(Int(min/grainSize)..<Int(max/grainSize))

Also helpful post about ranges in swift 3.

Upvotes: 1

adev
adev

Reputation: 2092

Try this in Swift 3, it will work. There are no init(start: end:) method in Range. That is the reason for this error.

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)))

This creates following range:

-300..<300

Upvotes: 1

Related Questions