Range class of Swift 3.0


Explain the usage of the range class in the Migrating to Swift 2.3 or Swift 3 from Swift 2.2 article.

Described as follows:

In support of the collections changes, Range types also had some changes. Previously x..<y and x...y produced the same type, Range<T>. Now these expressions can produce one of the four types: Range, CountableRange, ClosedRange, CountableClosedRange. We split Range into Range and ClosedRange types to allow closed ranges that include the maximum value of the type (for example, 0...Int8.max works now). The plain range types and their ~Countable counterparts differ in the capabilities:

  • Range<Bound> and ClosedRange<Bound> now only require Comparable for the bound. This allows you to create a Range<String>.
  • Range and ClosedRange can’t be iterated over (they are not collections anymore), since a value that is merely Comparable cannot be incremented.
  • CountableRange and CountableClosedRange require Strideabe from their bound and they conform to Collection so that you can iterate over them.

The ..< and ... operators try to do the right thing and return the most capable range, so that code like for i in 1..<10 infers a CountableRange and continues to work. If you have a variable that is typed as one range type, and you need to pass it to an API that accepts a different type, use the initializers on range types to convert:

var r = 0..<10 // CountableRange<Int>
Range(r) // converts to Range<Int>

From Range<Bound> and ClosedRange<Bound> now only require Comparable for the bound. This allows you to create a Range<String>.
We can create a String generic Range.

// Range("a"..<"f")
let rangeStr = Range<String>.init(uncheckedBounds: ("a", "f"))
rangeStr.contains("b") // true

From Range and ClosedRange can’t be iterated over (they are not collections anymore), since a value that is merely Comparable cannot be incremented.
ClosedRange can't be used for iteration.

let closeRange = ClosedRange(uncheckedBounds: (1, 2))
for i in closeRange {
}

// error: type 'ClosedRange<Int>' does not conform to protocol 'Sequence'

From CountableRange and CountableClosedRange require Strideabe from their bound and they conform to Collection so that you can iterate over them.
CountableRange and CountableClosedRange can be used for iteration.

let myAry = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
let countableRange = CountableRange(uncheckedBounds: (1, 2))
for i in countableRange {
    myAry[i] // 2
}
let countableCloseRange = CountableClosedRange(uncheckedBounds: (1, 2))
for i in countableCloseRange {
    myAry[i] // 2, 3
}

About String and Range:
In Swift 2.x, we can do this:

let myString = "Hello World"
let myRange = Range<String.Index>(start: myString.startIndex, end: myString.startIndex.advancedBy(5))
let mySubString = myString.substringWithRange(myRange)   // Hello

// or simply

let myString = "Hello World"
let myRange = myString.startIndex..<myString.startIndex.advancedBy(5)
let mySubString = myString.substringWithRange(myRange)   // Hello

But in the Swift 3.0, the advancedBy is unavailable, we need use index(_:offsetBy:).

let myString = "Hello World"
let myRange = myString.startIndex..<myString.index(myString.startIndex, offsetBy: 5)
let mySubString = myString.substring(with: myRange)   // Hello
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,141評(píng)論 0 23
  • 事業(yè)的“事”,是喜歡的事!
    絡(luò)琳閱讀 134評(píng)論 0 0
  • 王小跳x閱讀 203評(píng)論 0 0
  • 昨天和朋友看完了董子健和李夢(mèng)主演的《少年巴比倫》。去的路上一直在找自己最后一門不會(huì)掛科的據(jù)點(diǎn),微小到我把考試信息全...
    阿芥閱讀 737評(píng)論 0 8

友情鏈接更多精彩內(nèi)容