- Hands-On Design Patterns with Swift
- Florent Vilmart Giordano Scalzo Sergio De Simone
- 171字
- 2021-07-02 14:45:08
Working with ranges
Ranges come in two flavors: Range and ClosedRange. The difference between Range and ClosedRange is the inclusion of the upper bound. In a ClosedRange, the upper bound is included; in Range, it isn't.
If you want to include all numbers between 0 and 10, not including 10, you can represent it in two ways, as follows:
let range1 = 0..<10
let range2 = 0...9
range1.contains(10) // false
The two ranges are equivalent, and they contain the same numbers. They read differently and carry different meanings, however depending on what you want to express, you may want to pick one over the other. Ranges work with the Comparable protocol, which means that any type that conforms to this Comparable protocol is suited for creating ranges.
For example, you could create a Range type of Strings, as follows:
let uppercased = "A"..."Z"
uppercased.contains("C") // true
uppercased.contains("c") // false
You can also use the ~= operator, which is an alias for the contains method:
uppercased ~= "Z" // true
- Redis使用手冊
- 大規(guī)模數(shù)據(jù)分析和建模:基于Spark與R
- 信息系統(tǒng)與數(shù)據(jù)科學
- MySQL從入門到精通(第3版)
- 大數(shù)據(jù)時代下的智能轉型進程精選(套裝共10冊)
- 數(shù)據(jù)庫原理與應用(Oracle版)
- Microsoft Power BI數(shù)據(jù)可視化與數(shù)據(jù)分析
- 數(shù)字媒體交互設計(初級):Web產品交互設計方法與案例
- 達夢數(shù)據(jù)庫運維實戰(zhàn)
- 企業(yè)級容器云架構開發(fā)指南
- MySQL技術內幕:SQL編程
- 新手學會計(2013-2014實戰(zhàn)升級版)
- 區(qū)域云計算和大數(shù)據(jù)產業(yè)發(fā)展:浙江樣板
- 爬蟲實戰(zhàn):從數(shù)據(jù)到產品
- 數(shù)據(jù)庫應用系統(tǒng)技術