How to increase String.Index in swift3?

In swift2.3 ++ to increase string.index

eg. i ++

I changed to quick 3 that the code "Unary operator '++' cannot be applied to an operand of type '@lvalue String.Index' (aka '@lvalue String.CharacterView.Index')" in swift 3

I rewrite for example. i + = 1

but this code cannot solve. Please help me.

+11
source share
2 answers

String.Index is the font for String.CharacterView.Index . The index itself cannot be increased. Rather, you can use the index(after:) instance method in CharacterView , which you used to retrieve the index.

For instance.:

 let str = "foobar" let chars = str.characters if let bIndex = chars.index(of: "b") { let nextIndex = chars.index(after: bIndex) print(str[bIndex...nextIndex]) // ba } 

Or, if you have an index (e.g. str.startIndex ), you can use the index(_:, offsetBy:) instance method index(_:, offsetBy:) , also available directly for String instances:

 let str = "foobar" let startIndex = str.startIndex let nextIndex = str.index(startIndex, offsetBy: 1) print(str[startIndex...nextIndex]) // fo 
+13
source

String.Index does not have ++ , += or any kind of operator (except comparisons, for example < , > , == ) defined for it. It has other methods defined for moving the index. To increase the position of index 1, the code will look like this: string.index(i, offsetBy: 1)

 let string = "Some string" var i = string.startIndex i = string.index(i, offsetBy: 1) 
+5
source

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


All Articles