for-in循环快速4个两个变量和增量

时间:2019-02-23 21:31:41

标签: swift for-loop swift4 for-in-loop

我想问一个关于Swift 4中的for-in循环的问题。我想设置两个变量及其增量:

j = 1,f = 87.5; j (-90); j ++,f-= 2.5 {}

如何将其转换为Swift 4?我希望很快能收到您的回音!

2 个答案:

答案 0 :(得分:3)

您要遍历两个序列,并在最短时间用尽时停止。第一个序列是1..<numberOfGrids。第二个是“值从87.5到-90乘-2.5”,即stride(from: 87.5, to: -90, by: -2.5)

要遍历两个序列(最短时间用尽时停止),请使用zip:

let grids = 1..<numberOfGrids
let fs = stride(from: 87.5, to: -90, by: -2.5) // not sure what "f" represents
for (j, f) in zip(grids, fs) {
    print(j, f)
}

答案 1 :(得分:0)

您可以尝试以下代码:

let numberOfGrid = 100
var j = 1
var f = 87.5
repeat {
    // Do something here
    j += 1
    f -= 2.5
    // Do something here
} while j < numberOfGrid && f > -90