Swift-基础语法区间类型
2020-11-16 本文已影响0人
守护地中海的花
ClosedRange
、Range
let range0 = 1...3
let range1:ClosedRange<Int> = 1...3
let range2:Range<Int> = 1..<3
let range3:PartialRangeThrough<Int> = ...5
for i in range2 {
print(i)
}
print(range1.contains(2))
- contains包含返回Bool值
字符、字符串也能使用区间运算,但默认不能用在for-in中
image.pnglet stringRange = "cc"..."ff"
print(stringRange.contains("aa"))
print(stringRange.contains("dd"))
控制台:
false
true
带间隔区间值stride
stride:跨步
let hour = 11
let hourInterval = 2
for i in stride(from: 4, through: hour, by: hourInterval) {
print(i)
}
for i in stride(from: 4, to: hour, by: hourInterval) {
print(i)
}
控制台:
4
6
8
10
4
6
8
10