How to increment a variable of type String.CharacterView.Index?

I tried to do this (on the playground):

class SomeClass {

    private let input: String.CharacterView
    private var position: String.CharacterView.Index

    ...

    private func advance() {

        position += 1

    } // advance

} // SomeClass

... but I get the following error message:

error: binary operator '+ =' cannot be applied to operands of type 'String.CharacterView.Index' and 'Int'

The reason I am trying to increase this index is because I would like to do something like:

input[position]

... and access a specific character using input .

At this point, I already know that I cannot use this binary operator on a position . What would be a viable solution?

I am using Swift 3 and Xcode Version 8.0 beta 6 (8S201h)

+4
source share
1

Swift 3 " ",   Swift.

private func advance() {
    input.formIndex(&position, offsetBy: 1)
}

. endIndex,

private func advance() {
    input.formIndex(&position, offsetBy: 1, limitedBy: input.endIndex)
}
+4

Source: https://habr.com/ru/post/1654306/


All Articles