0

This worked fine in Swift 3.2:

NSRange(location:myString.startIndex.distance(to: myIndex),length:myLength)

But in Swift 4 it gives the error in the title. What do?

stbss
  • 3
  • 1

1 Answers1

1

Slicing strings has changed quite a bit in Swift 4. NSRange is generally not the right tool anymore, so how to fix this depends on the rest of the surrounding code. That said, the equivalent code in Swift 4 is this:

NSRange(location:myString.distance(from: myString.startIndex, to: myIndex),length:myLength)

See the String Manifesto for how Strings are intended to work going forward. See SE-0163 and SE-0180 for more of the specific changes in 4.0.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610