Can't create a range in Swift 3

后端 未结 1 1327
清酒与你
清酒与你 2020-12-15 06:49

I am trying to make a range in Swift 3 that I already had in Swift 2 but it keeps giving me this error: String may not be indexed with \'Int\', it has variable size e

相关标签:
1条回答
  • 2020-12-15 07:27

    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).

    0 讨论(0)
提交回复
热议问题