gpt4 book ai didi

swift - 更新另一个内部步幅循环的索引

转载 作者:行者123 更新时间:2023-11-28 14:03:07 25 4
gpt4 key购买 nike

由于计算的复杂性,我必须在多个 for 循环 中更新索引。

所以,

for index in stride(from: 0, to: data.count, by: 1)
.....
.....

for i in stride(from: index, to: data.count, by: 1)
.....
index=i ; break; //update index and break inner loop so next outer for loop iteration will start from a new index.

因此外循环现在将跳转到一个新值。

在循环中使用 "where" 是不可能的,因为内循环中的条件取决于太多的东西。 (这不仅仅是 i != 2)

  1. 我会得到一个错误,指出索引已被允许
  2. 这是一种不好的做法吗?

最佳答案

您可以通过用 while 替换第一个步幅来实现。

var index = 0

while index < data.count {
.....
.....
index += 1
for i in stride(from: index-1, to: data.count, by: 1) {
.....
index=i
break
}
}

关于swift - 更新另一个内部步幅循环的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53331106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com