[swift-evolution] What's the best replacement if "Remove C-style for loops"?

Pyry Jahkola pyry.jahkola at iki.fi
Thu Mar 17 07:54:57 CDT 2016


> On 17 Mar 2016, at 14:48, nebulabox via swift-evolution <swift-evolution at swift.org> wrote:
> 
> I have several codes like the following:
> 
> for var i = 0; i < myarr.count; i += n { // the step is NOT 1
>    // many codes
> }
> 
> (…) If no C-style for loop,  what's the best replacement for it ?

The stride is what you're looking for:

for i in stride(from: 0, to: myarr.count, by: n) {
    // ...
}

In the current Swift release, you'd construct it like this though:

for i in 0.stride(to: myarr.count, by: n) {
    // ...
}

— Pyry

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160317/ac61c93d/attachment.html>


More information about the swift-evolution mailing list