- Reactive Programming with Swift 4
- Navdeep Singh
- 172字
- 2021-06-24 18:58:00
One sided ranges
Swift 4 makes it optional to provide a starting index or finishing index of ranges, as used with earlier versions of Swift.
With earlier versions of Swift, you had to do the following to use ranges:
let contactNames = [“Alex”, “Ravi”, “Moin”, “Vlad”, “Mark”]
let firstTwoContacts = contactNames[0..<2]
let lastThreeContacts = contactNames[2..<contactNames.count]
print(firstTwoContacts)
print(lastThreeContacts)
You will get the result as follows:
["Alex", "Ravi"] , for [0..<2]
["Moin", "Vlad", "Mark"], for [2..<contactNames.count]
However, with Swift 4, you no longer have to be constrained to lower bounds or upper bounds of the range mentioned in the for loop mentioned earlier in the code example. You can now use a one sided range where the missing side will automatically be treated as the start or end of a sequence:
let firstTwoContacts = contactNames[..<2]
let lastThreeContacts = contactNames[2...]
print(firstTwoContacts)
print(lastThreeContacts)
You will get the result as shown:
["Alex", "Ravi"] , for [..<2]
["Moin", "Vlad", "Mark"] for [2...]
推薦閱讀
- Angular UI Development with PrimeNG
- 自己動手寫Java虛擬機(jī)
- 深入淺出Java虛擬機(jī):JVM原理與實戰(zhàn)
- PyTorch自然語言處理入門與實戰(zhàn)
- 軟件項目管理實用教程
- WebRTC技術(shù)詳解:從0到1構(gòu)建多人視頻會議系統(tǒng)
- SQL經(jīng)典實例(第2版)
- 焊接機(jī)器人系統(tǒng)操作、編程與維護(hù)
- Scala Reactive Programming
- Kotlin開發(fā)教程(全2冊)
- Android移動開發(fā)案例教程:基于Android Studio開發(fā)環(huán)境
- 深度學(xué)習(xí)原理與PyTorch實戰(zhàn)(第2版)
- R語言:邁向大數(shù)據(jù)之路(加強(qiáng)版)
- Mastering Embedded Linux Programming
- Java程序設(shè)計實用教程(第2版)