Michel
Michel

Reputation: 11761

No '...' candidates produce 'Range<String.Index>'

While converting an old iOS app to Sift 3.0 I hit the following issue: The code is:

cutRange = numberString.index(numberString.startIndex, offsetBy:2)...numberString.index(numberString.startIndex, offsetBy:5)

The error message I get is:

No '...' candidates produce the expected contextual result type 'Range<String.Index>' (aka 'Range<String.CharacterView.Index>')

I have seen a few post related to the subject, but was not very satisfied.

So what is the simplest way to solve this problem?

Upvotes: 0

Views: 343

Answers (1)

OOPer
OOPer

Reputation: 47906

In Swift 3, two range operators generate different results:

  • closed range operator ... -> ClosedRange (by default)
  • (half open) range operator ..< -> Range (by default)

So, assuming your cutRange is declared as Range<String.Index>, you need to use half open range operator ..<:

cutRange = numberString.index(numberString.startIndex, offsetBy:2)..<numberString.index(numberString.startIndex, offsetBy:6)

(Please do not miss the last offset is changed to 6.)

Upvotes: 3

Related Questions