- 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
- Access 2016數(shù)據(jù)庫教程(微課版·第2版)
- Access 2007數(shù)據(jù)庫應(yīng)用上機(jī)指導(dǎo)與練習(xí)
- 數(shù)據(jù)庫原理與應(yīng)用(Oracle版)
- Remote Usability Testing
- Python金融實(shí)戰(zhàn)
- Oracle PL/SQL實(shí)例精解(原書第5版)
- 數(shù)據(jù)科學(xué)工程實(shí)踐:用戶行為分析與建模、A/B實(shí)驗(yàn)、SQLFlow
- 數(shù)據(jù)庫原理與應(yīng)用
- R Object-oriented Programming
- 深入理解InfluxDB:時(shí)序數(shù)據(jù)庫詳解與實(shí)踐
- 菜鳥學(xué)SPSS數(shù)據(jù)分析
- R Machine Learning Essentials
- 企業(yè)級大數(shù)據(jù)項(xiàng)目實(shí)戰(zhàn):用戶搜索行為分析系統(tǒng)從0到1
- 數(shù)據(jù)應(yīng)用工程:方法論與實(shí)踐
- 數(shù)據(jù)中臺(tái)實(shí)戰(zhàn):手把手教你搭建數(shù)據(jù)中臺(tái)