2

Can anyone tell me why the code below works in Swift 2, but somehow breaks in Swift 3?

let range: Range = 0...2

However it can simply be fixed by doing this

let range: Range = 0..<3

Anyone knows what is the reason behind this?

Chan Jing Hong
  • 2,251
  • 4
  • 22
  • 41
  • http://stackoverflow.com/questions/36157777/initstartend-is-deprecated-it-will-be-removed-in-swift-3-use-the-o – Bista Oct 01 '16 at 11:18
  • 1
    A very good example for the nonsense of explicit type annotations. If the compiler needs help it will tell you (e.g. *ambiguous use of something...*). – vadian Oct 01 '16 at 11:22

1 Answers1

4

Operators ... and ..< used to produce the same type, Range, in Swift 2.x. Now they produce different types (migration guide):

  • Range
  • CountableRange
  • ClosedRange
  • CountableClosedRange

Changing the type in the first assignment to ClosedRange should fix the problem. Better yet, let Swift infer the type for you:

let range = 0...2
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523