【问题标题】:Swift 4: Cannot subscript a value of type 'String' with an index of type 'CountablePartialRangeFrom<Int>'Swift 4:无法使用“CountablePartialRangeFrom<Int>”类型的索引为“String”类型的值下标
【发布时间】:2017-11-14 23:34:51
【问题描述】:

所以我有这个快速代码:

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

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

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

Cannot subscript a value of type 'String' with an index of type 'CountablePartialRangeFrom&lt;Int&gt;'

这似乎应该很明显,有人可以帮忙吗?

【问题讨论】:

标签: swift range substring


【解决方案1】:

哎呀。看来我只需要这样做:

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

【讨论】:

  • 你也可以说let subsets = response.dropFirst(11) :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-29
  • 1970-01-01
  • 2018-04-06
  • 2017-03-30
相关资源
最近更新 更多