Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Stride in Swift 2.0

I'm trying to figure out how to use the Stride features in Swift.

It seems to have changed again, since Xcode 7.0 beta 6.

Previously I could use

let strideAmount = stride(from: 0, to: items.count, by: splitSize) let sets = strideAmount.map({ clients[$0..<advance($0, splitSize, items.count)] }) 

Now, despite the code hint I cannot figure out how to use this feature.

Any examples would be helpful thanks.

I've looked at examples, but I cannot come to grips with how to use it. All I get from the Apple Docs are limited.

Thanks

like image 699
DogCoffee Avatar asked Aug 25 '15 06:08

DogCoffee


People also ask

What is stride function in Swift?

Swift has a helpful stride() , which lets you move from one value to another using any increment – and even lets you specify whether the upper bound is exclusive or inclusive.

For what purpose the stride from to by :) function is used?

stride(from:to:by:)Returns a sequence from a starting value to, but not including, an end value, stepping by the specified amount.


1 Answers

It changed a bit, here is the new syntax:

0.stride(to: 10, by: 2) 

and

Array(0.stride(to: 10, by: 2)) // is [0, 2, 4, 6, 8] 

if you take a look at here, you can see what types conform to the Strideable protocol.

As @RichFox pointed out, in Swift 3.0 the syntax changed back to the original global function form like:

stride(from:0, to: 10, by: 2) 
like image 136
Dániel Nagy Avatar answered Sep 28 '22 13:09

Dániel Nagy