I'm using the MapBox navigation framework that hasn't quite been updated to Swift 4. I have one 'subscript' error that I can't quite get around. Here's the code. I'd really appreciate any help. Thank you.
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
}
The error message is misleading. The real problem is that subscripting
a String.UnicodeScalarView
with a range returns a String.UnicodeScalarView.SubSequence
, so you cannot assign that back to
encodedString
.
One solution would be to create a String.UnicodeScalarView
from the subsequence:
encodedString = String.UnicodeScalarView(encodedString[encodedString.index(after: currentIndex)...])
Alternatively (and perhaps simpler) go the other way around and
remove the initial part of encodedString
instead:
encodedString.removeSubrange(...currentIndex)
In either case, you can take use "one-sided ranges", compare SE-0172