Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我必须修改我的 swift 2 应用程序的 for 循环。目前我使用这种语法
for (var x = 0; x < 5; x++) {
我了解到我必须使用这个:
for x in 0..<5 {
但我必须如何改变这个for循环:
for (var x = 0; x < 6; x = x+2) {
使用stride功能
stride
// for x<6 for i in 0.stride(to: 6, by: 2) { print(i) // 0,2,4 } //for x<=6 for i in 0.stride(through: 6, by: 2) { print(i) // 0,2,4,6 }
以简单的方式试试这个,
var x = 0 for x in 0..<5 { x += 2 } print(x)
检查此链接以获取更多参考Swift 2.2 之旅