- Reactive Programming with Swift 4
- Navdeep Singh
- 137字
- 2021-06-24 18:58:00
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.
推薦閱讀
- Mastering ServiceStack
- 深入淺出Electron:原理、工程與實(shí)踐
- Cross-platform Desktop Application Development:Electron,Node,NW.js,and React
- Python語(yǔ)言程序設(shè)計(jì)
- Flux Architecture
- Java Web開發(fā)詳解
- Terraform:多云、混合云環(huán)境下實(shí)現(xiàn)基礎(chǔ)設(shè)施即代碼(第2版)
- Python機(jī)器學(xué)習(xí)算法: 原理、實(shí)現(xiàn)與案例
- 學(xué)習(xí)OpenCV 4:基于Python的算法實(shí)戰(zhàn)
- Node學(xué)習(xí)指南(第2版)
- Qt 4開發(fā)實(shí)踐
- Python機(jī)器學(xué)習(xí)與量化投資
- 計(jì)算機(jī)組裝與維護(hù)(第二版)
- 嵌入式C編程實(shí)戰(zhàn)
- Linux Networking Cookbook