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

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.

主站蜘蛛池模板: 桃江县| 新巴尔虎左旗| 嘉鱼县| 开远市| 安化县| 昌黎县| 彰化市| 宣威市| 永新县| 民丰县| 乐平市| 贞丰县| 星子县| 尤溪县| 阆中市| 吐鲁番市| 临猗县| 当涂县| 清涧县| 永靖县| 门源| 靖宇县| 板桥市| 浮山县| 高平市| 文成县| 闽清县| 双辽市| 永城市| 安仁县| 上栗县| 闽侯县| 宁蒗| 白城市| 沙湾县| 汝城县| 凉城县| 喜德县| 天水市| 武清区| 衡水市|