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

Pattern matching with one sided ranges.

Pattern matching works really well with one sided ranges in switch statements, but you should be mindful of the one hitch that it has.

While writing a switch case, be careful to add a default case since you have to make your switch case exhaustive and since one sided ranges are infinite now, adding a default case becomes mandatory:

let selectedNumber = 7
switch selectedNumber {
case ..<0 :
print("You have selected a negative number.")
case 0... :
print("You have selected a positive number")
default :
break
}

Here, note that we have already covered all the scenarios in the first 2 cases:

  • Case 1: All negative numbers up to -1
  • Case 2: All positive numbers from 0 onward

Hence, we simply break out the switch statement in the default case.

主站蜘蛛池模板: 陇西县| 上饶市| 桐庐县| 揭东县| 青州市| 渭南市| 睢宁县| 毕节市| 阿克苏市| 沾化县| 澄城县| 定西市| 阳泉市| 祁门县| 阳谷县| 平乐县| 纳雍县| 新干县| 高要市| 师宗县| 西安市| 那曲县| 资源县| 阳山县| 旌德县| 新民市| 松原市| 井陉县| 驻马店市| 固始县| 页游| 崇信县| 绥化市| 保亭| 张掖市| 福泉市| 吉安县| 临夏市| 安庆市| 贡觉县| 海盐县|