9

So I have this swift code:

func didReceiveResponse(response:String) {
  ...
  let substr = response[11...]

By my interpretation, substr should be a Substring referencing all characters after index 11 in the response string.

What actually happens is this compiler error:

Cannot subscript a value of type 'String' with an index of type 'CountablePartialRangeFrom<Int>'

This seems like it should be obvious, can anyone help please?

Orion Edwards
  • 121,657
  • 64
  • 239
  • 328
  • 3
    `String` is indexed by `String.Index`, not `Int` – see for example https://stackoverflow.com/q/39676939/2976878 – Hamish Nov 14 '17 at 23:37

1 Answers1

10

Whoops. Seems I needed to just do this:

let idx = response.index(response.startIndex, offsetBy: 11)
let substr = response[idx...]
Orion Edwards
  • 121,657
  • 64
  • 239
  • 328