在 Swift 2.0 中使用 Stride [英] Using Stride in Swift 2.0
问题描述
我正在研究如何在 Swift 中使用 Stride 功能.
I'm trying to figure out how to use the Stride features in Swift.
从 Xcode 7.0 beta 6 开始似乎又发生了变化.
It seems to have changed again, since Xcode 7.0 beta 6.
以前我可以使用
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.
我看过示例,但我无法理解如何使用它.我从 Apple Docs 中得到的都是有限的.
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.
谢谢
推荐答案
有点变化,这里是新的语法:
It changed a bit, here is the new syntax:
0.stride(to: 10, by: 2)
和
Array(0.stride(to: 10, by: 2)) // is [0, 2, 4, 6, 8]
如果您查看此处,您可以看到哪些类型符合Strideable
协议.
if you take a look at here, you can see what types conform to the Strideable
protocol.
正如@RichFox 指出的那样,在 Swift 3.0 中,语法变回了原始的全局函数形式,例如:
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)
这篇关于在 Swift 2.0 中使用 Stride的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!