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

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.

主站蜘蛛池模板: 东兴市| 五华县| 葵青区| 锡林郭勒盟| 杨浦区| 资兴市| 沂源县| 固镇县| 乌兰察布市| 彰武县| 泽库县| 津市市| 兴安盟| 轮台县| 淮北市| 南乐县| 九龙县| 息烽县| 南雄市| 都兰县| 缙云县| 青河县| 金寨县| 内黄县| 自治县| 兴和县| 襄垣县| 鄂托克前旗| 陈巴尔虎旗| 成都市| 图木舒克市| 吉安县| 常山县| 屯门区| 大悟县| 滨海县| 贵南县| 株洲县| 鄄城县| 黄骅市| 陆良县|