Swift 4:'下标'不可用错误

时间:2017-11-08 18:29:22

标签: swift mapbox swift4 subscript swift-subscript

我正在使用尚未更新到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
}

enter image description here

1 个答案:

答案 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

相关问题