官术网_书友最值得收藏!

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...]
主站蜘蛛池模板: 台北县| 界首市| 三都| 资兴市| 监利县| 永嘉县| 安图县| 伊金霍洛旗| 通山县| 宁河县| 神木县| 铜鼓县| 竹北市| 长岛县| 泾阳县| 桓台县| 西贡区| 潢川县| 靖边县| 西乌珠穆沁旗| 永平县| 珲春市| 汝城县| 红原县| 湄潭县| 镶黄旗| 濉溪县| 琼海市| 宾川县| 定西市| 江源县| 泰来县| 铜山县| 宁阳县| 方城县| 灌阳县| 利津县| 孟州市| 新余市| 拉萨市| 武隆县|