Swift 4:无法使用类型为“CountablePartialRangeFrom ”的索引下标“String”类型的值

时间:2021-12-19 22:28:49

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.

根据我的解释,substr应该是一个子串,引用响应字符串中索引11之后的所有字符。

What actually happens is this compiler error:

实际发生的是这个编译器错误:

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

无法使用类型为“CountablePartialRangeFrom ”的索引下标“String”类型的值

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

这似乎应该是显而易见的,任何人都可以帮忙吗?

1 个解决方案

#1


6  

Whoops. Seems I needed to just do this:

哎呦。似乎我需要这样做:

let idx = response.index(response.startIndex, offsetBy: 11)
let substr = response[idx...]

#1


6  

Whoops. Seems I needed to just do this:

哎呦。似乎我需要这样做:

let idx = response.index(response.startIndex, offsetBy: 11)
let substr = response[idx...]