Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 201
Orion Edwards Avatar asked Nov 14 '17 23:11

Orion Edwards


1 Answers

Whoops. Seems I needed to just do this:

let idx = response.index(response.startIndex, offsetBy: 11)
let substr = response[idx...]
like image 141
Orion Edwards Avatar answered Sep 20 '22 11:09

Orion Edwards