stride与循环结构
区间运算符
开区间:[a,b]
闭区间:[a,b)
举例:
1.开区间
1 | for index in 1..<10{ |
index的值一次为:0~9;可以用此方法遍历数组。比如:
1 | var scoreArr = [22,33,44,55,66,77,88,99]; |
2.闭区间
1 | for index in 1...10{ |
index的值以此为:0~10
在swift3.0中stride语法已经改为这样:stride(from: , to: , by: )函数和stride(from: , through: , by: )函数
stride 方法定义在 Strideable 协议中,swift3.0废弃了传统C的for循环之后,定义的全新方法。
stride(from: , to: , by: )相当于开区间;
stride(from: , through: , by: )相当于闭区间
个人理解:
表示从初始数值到目标数值,by:表示跨越的进度(可加可减)。
举例:
1 | for index in stride(from: 10, through: 0, by: -1){ |
1 | for index in stride(from: 10, to: 0, by: -1){ |
index的数值:
index –10
index –9
index –8
index –7
index –6
index –5
index –4
index –3
index –2
index –1
用stride方法遍历scoreArr数组
1 | for i in stride(from: 0, to: scoreArr.count, by: 1){ |