- 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.
推薦閱讀
- 移動UI設計(微課版)
- 垃圾回收的算法與實現
- Apache Spark 2 for Beginners
- SQL語言從入門到精通
- 機械工程師Python編程:入門、實戰與進階
- SQL Server 2012數據庫管理與開發項目教程
- Learn React with TypeScript 3
- Learning Three.js:The JavaScript 3D Library for WebGL
- Web性能實戰
- 從程序員角度學習數據庫技術(藍橋杯軟件大賽培訓教材-Java方向)
- 深入實踐DDD:以DSL驅動復雜軟件開發
- 視窗軟件設計和開發自動化:可視化D++語言
- WCF全面解析
- JavaScript高級程序設計(第4版)
- PHP程序設計經典300例