- 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.
推薦閱讀
- Learning Microsoft Windows Server 2012 Dynamic Access Control
- 深度學(xué)習(xí)經(jīng)典案例解析:基于MATLAB
- Learning Spring 5.0
- Cross-platform Desktop Application Development:Electron,Node,NW.js,and React
- 數(shù)據(jù)結(jié)構(gòu)(Python語言描述)(第2版)
- AngularJS深度剖析與最佳實(shí)踐
- Java Web程序設(shè)計(jì)
- Web Development with MongoDB and Node(Third Edition)
- Python之光:Python編程入門與實(shí)戰(zhàn)
- OpenCV 4計(jì)算機(jī)視覺項(xiàng)目實(shí)戰(zhàn)(原書第2版)
- Webpack實(shí)戰(zhàn):入門、進(jìn)階與調(diào)優(yōu)
- Android開發(fā)三劍客:UML、模式與測試
- H5+移動營銷設(shè)計(jì)寶典
- INSTANT JQuery Flot Visual Data Analysis
- PostgreSQL Developer's Guide