Curnelious Curnelious - 15 days ago 4
Swift Question

Stride understanding

I have this code I took from some place :

for y in (0 as CGFloat).stride(from: 0.0, to: rect.height, by: elementSize)


problems :


  1. What is that?
    stride
    should look like
    for i in stride(from: 1, to: 10, by: 3)

  2. It gives errors of course, how can I make it work for
    Swift 3
    where
    y
    is a
    CGFloat
    ? when using these arguments in the right form I get errors about stride being taking only
    Ints
    ?


Answer

1). That is not Swift 3

2).

for y : CGFloat in stride(from: 0.0, to: 10.0, by: 0.5)
{
    print(y)
}

PS: The loop goes up to 9.5. If you need to reach 10.0 you need

for y : CGFloat in stride(from: 0.0, through: 10.0, by: 0.5) { ...