According to the Control Flow section in the Swift Language Guide,
The
for-in
loop performs a set of statements for each item in a range, sequence, collection, or progression.
I'm pretty sure I know what three of these are:
...
or ..<
SequenceType
protocol (documentation not anywhere obvious, but various people have reverse-engineered it)Array
and Dictionary
(I note #3 is probably redundant as Array
and Dictionary
both seem to conform to SequenceType
.)
But what's a "progression"? Is it some fourth thing, or was the writer just being wordy?
ETA: I see there's a CollectionType
protocol as well, so that explains #3.
The first mention I see of "progression" besides the for-in documentation is in the comments of the swift framework where the stride methods are defined.
func stride<T : Strideable>(from start: T, to end: T, by stride: T.Stride) -> StrideTo<T>
Return the sequence of values (
start
,start + stride
,start + stride + stride
, ... last) where last is the last value in the progression that is less thanend
.
func stride<T : Strideable>(from start: T, through end: T, by stride: T.Stride) -> StrideThrough<T>
Return the sequence of values (
start
,start + stride
,start + stride + stride
, ... last) where last is the last value in the progression less than or equal toend
. Note:: There is no guarantee thatend
is an element of the sequence.
So in short, "progression" refers to the Strideable
protocol similar to how "collection" refers to the CollectionType
protocol and the classes and structs that conform to it.
Numeric types (Int, Double, Float, UnsafePointer, Bit, etc...) tend to conform to this protocol so they may be incremented/decremented for for in
loops. Full inheritance graph for the Strideable
protocol found here.
What you are probably looking for is this kind of loop ( for in stride )
for i in stride(from: 1, to: 10, by: 2) {
println(i)
}
that is the new syntax replacement for
for var i = 1; i < 10; i += 2 {
println(i)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With