Cannot convert value of type 'Range<Int>?' to expected argument type 'Range<String.Index>?

Viewed 598

I'm getting an error when trying to pass into string.range(of:, options:, range:, locale)

Specifically, for the range modifier I tried passing in Range(remainingRange) with remainingRange = NSMakeRange(0, string.count)

However, the error seems to indicate it's of a mismatched type where there is no String.Index: 'Range<Int>?' to expected argument type 'Range<String.Index>?'.

I'm not sure why this would be an issue as just passing the range in objective c works. Any ideas?

1 Answers

Swift strings cannot be subscripted by Int indexes because the length of the characters are flexible.

Replace

NSMakeRange(0, string.count)

with

NSRange(string.startIndex..., in: string)

or – better – just omit the range and the locale parameters.

Related