有人可以解釋爲什麼半開和關閉範圍不再在Swift 3中的字符串上工作相同?範圍運算符(.. <和...)在Swift字符串
此代碼:
var hello = "hello"
let start = hello.index(hello.startIndex, offsetBy: 1)
let end = hello.index(hello.startIndex, offsetBy: 4)
let range = start..<end // <-- Half Open Range Operator still works
let ell = hello.substring(with: range)
但這並不:
var hello = "hello"
let start = hello.index(hello.startIndex, offsetBy: 1)
let end = hello.index(hello.startIndex, offsetBy: 4)
let range = start...end // <-- Closed Range Operator does NOT work
let ello = hello.substring(with: range) // ERROR
這導致錯誤如下所示:
Cannot convert value of type 'ClosedRange<String.Index>' (aka 'ClosedRange<String.CharacterView.Index>') to expected argument type 'Range<String.Index>' (aka 'Range<String.CharacterView.Index>')