In Swift 3, "Collections move their index", see
A New Model for Collections and Indices on Swift evolution.
Here is an example for String ranges and indices:
let string = "ABCDEFG"
if let range = string.range(of: "CDEF") {
let lo = string.index(range.lowerBound, offsetBy: 1)
let hi = string.index(range.lowerBound, offsetBy: 3)
let subRange = lo ..< hi
print(string[subRange]) // "DE"
}
The
public func index(_ i: Index, offsetBy n: IndexDistance) -> Index
method is called on the string to calculate the new indices from the
range (which has properties lower/upperBound
now instead of
start/endIndex
).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…