That function increments the start
index by n
positions, but not
beyond the end
index.
Example: You want to truncate strings to a given maximal length:
func truncate(string : String, length : Int) -> String {
let index = advance(string.startIndex, length, string.endIndex)
return string.substringToIndex(index)
}
println(truncate("fooBar", 3)) // foo
println(truncate("fo", 3)) // fo
In the first call, the start index is incremented by 3 positions,
in the second example only by two. With
let index = advance(string.startIndex, length)
the second call would crash with a runtime exception, because
a string index must not be advanced beyond the end index.
solved How to use advance function in swift with three parameters?