区間演算子を適用して作られた構造体
区間演算子を適用して作られた構造体は3種類ある。
ジェネリックのパラメータ型には整数型を指定できる。
半開区間型
HalpOpenInterval<T>
case文で使える
閉区間型
ClosedInterval<T>
case文で使える
範囲型
Range<T>
for-in文で使える
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
let h1_1:HalfOpenInterval <Int> = 0..<10 let c1_1:ClosedInterval <Int> = 0...10 let r_1:Range<Int> = 0..<10 let h1_2 = HalfOpenInterval <Int> (0,10) let c1_2 = ClosedInterval <Int> (0,10) let r_2 = Range<Int> (start:0,end:10) let i = 10 switch i { case h1_1: print("h1:\(i)") case c1_1: print("c1:\(i)") //ここを通る default:break } for j in r_2 { print("\(j)") //0123456789 } |
Stride型
範囲を指定できて、増分を2以上にもできて、値を減らすこともできる型
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
let stride1 = stride(from: 0, through: 10, by: 2) let stride2 = stride(from: 0, to: 10, by: 2) let stride3 = stride(from: 10, to: 0, by: -2) for i in stride1 { print(i) //0246810 } for i in stride2 { print(i) //02468 } for i in stride3 { print(i) //108642 } |