- 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.
推薦閱讀
- Java程序設計實戰教程
- Node.js 10實戰
- 嵌入式軟件系統測試:基于形式化方法的自動化測試解決方案
- Mastering Zabbix(Second Edition)
- 樂學Web編程:網站制作不神秘
- Magento 2 Theme Design(Second Edition)
- 趣學Python算法100例
- Getting Started with Python Data Analysis
- HTML5入門經典
- TypeScript項目開發實戰
- SQL 經典實例
- Python項目實戰從入門到精通
- 基于GPU加速的計算機視覺編程:使用OpenCV和CUDA實時處理復雜圖像數據
- MySQL數據庫應用實戰教程(慕課版)
- 實戰Python網絡爬蟲