Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloads for '...' exist with these result types: ClosedRange<Bound>, CountableClosedRange<Bound>

Swift 2

let gap = CGFloat(randomInRange(StackGapMinWidth...maxGap))

Missing argument label 'range:' in call

Swift 3 - new error

let gap = CGFloat(randomInRange(range: StackGapMinWidth...maxGap))

No '...' candidates produce the expected contextual result type 'Range'

Overloads for '...' exist with these result types: ClosedRange, CountableClosedRange

like image 901
Edison Avatar asked Jun 20 '16 06:06

Edison


1 Answers

As of Swift 3, ..< and ... produce different kinds of ranges:

  • ..< produces a Range (or CountableRange, depending on the underlying type) which describes a half-open range that does not include the upper bound.
  • ... produces a ClosedRange (or CountableClosedRange) which describes a closed range that includes the upper bound.

If the randomInRange() calculates a random number in the given range, including the upper bound, then it should be defined as

func randomInRange(range: ClosedRange<Int>) -> Int {
    // ...
}

and you can call it as

let lo = 1
let hi = 10
let r = randomInRange(range: lo ... hi)
like image 95
Martin R Avatar answered Oct 10 '22 02:10

Martin R