无法在 Swift 中创建范围 3
Can't create a range in Swift 3
我正在尝试在 Swift 3 中创建一个我已经在 Swift 2 中设置的范围,但它一直给我这个错误:
String may not be indexed with 'Int', it has variable size elements
这是我的代码:
let range = expireRange!.startIndex.advancedBy(n: 7) ..< expireRange!.startIndex.advancedBy(n: 16)
expiredRange 是一个 Range<Index>?
在 Swift 2 中,我有:
let range = expireRange!.startIndex.advancedBy(7)...expireRange!.startIndex.advancedBy(16)
在Swift3中,"Collections move their index",见
A New Model for Collections and Indices Swift 进化。
这是字符串范围和索引的示例:
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"
}
public func index(_ i: Index, offsetBy n: IndexDistance) -> Index
方法在字符串上被调用以从
范围(现在具有属性 lower/upperBound
而不是
start/endIndex
).
我正在尝试在 Swift 3 中创建一个我已经在 Swift 2 中设置的范围,但它一直给我这个错误:
String may not be indexed with 'Int', it has variable size elements
这是我的代码:
let range = expireRange!.startIndex.advancedBy(n: 7) ..< expireRange!.startIndex.advancedBy(n: 16)
expiredRange 是一个 Range<Index>?
在 Swift 2 中,我有:
let range = expireRange!.startIndex.advancedBy(7)...expireRange!.startIndex.advancedBy(16)
在Swift3中,"Collections move their index",见 A New Model for Collections and Indices Swift 进化。
这是字符串范围和索引的示例:
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"
}
public func index(_ i: Index, offsetBy n: IndexDistance) -> Index
方法在字符串上被调用以从
范围(现在具有属性 lower/upperBound
而不是
start/endIndex
).