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. What can I try next?

1

1 Answer 1

10

Whoops. Seems I needed to just do this:

let idx = response.index(response.startIndex, offsetBy: 11)
let substr = response[idx...]
Sign up to request clarification or add additional context in comments.

1 Comment

You could also say let subsets = response.dropFirst(11) :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.