我正在使用尚未更新到Swift 4的MapBox导航框架。我有一个'下标'错误,我无法解决。这是代码。我真的很感激任何帮助。谢谢。
private func extractNextChunk(_ encodedString: inout String.UnicodeScalarView) throws -> String {
var currentIndex = encodedString.startIndex
while currentIndex != encodedString.endIndex {
let currentCharacterValue = Int32(encodedString[currentIndex].value)
if isSeparator(currentCharacterValue) {
let extractedScalars = encodedString[encodedString.startIndex...currentIndex]
encodedString = encodedString[encodedString.index(after: currentIndex)..<encodedString.endIndex]
return String(extractedScalars)
}
currentIndex = encodedString.index(after: currentIndex)
}
throw PolylineError.chunkExtractingError
}
答案 0 :(得分:4)
错误消息具有误导性。真正的问题是下标
带有范围的String.UnicodeScalarView
会返回String.UnicodeScalarView.SubSequence
,因此您无法将其分配回来
encodedString
。
一种解决方案是创建String.UnicodeScalarView
来自子序列:
encodedString = String.UnicodeScalarView(encodedString[encodedString.index(after: currentIndex)...])
或者(也许更简单)走另一条路
删除encodedString
的初始部分:
encodedString.removeSubrange(...currentIndex)
在任何一种情况下,您都可以使用&#34;单侧范围&#34;,比较 SE-0172