ios - 如何将变量设置为从 0 开始并通过嵌套 for 循环 Swift 2 中的每个循环获得一个级别

标签 ios swift2 nested-loops

所以这是 swift 2 中嵌套 for 循环的一个例子。在它的正上方和正下方有一个 for 循环,一个接一个地工作。每次调用 for 循环时,我都想爬一个级别。通常,如果这是一个 for 循环,它会自动发生,但由于它是一个嵌套的 for 循环,我需要它运行一次,然后跳转到下一个 for 循环。

问题是 var u 为 0,它总是设置为 0,并且总是从那里开始。有没有办法让它变成 post place[u] = place[++] 的形式?

placeLoop: for aPlace in place {
                print("\(aPlace)")
                print(" ")
                var u : Int = 0
                if aPlace == place[u] {
                    place[u] = place[++u]
                    //This is the manual way to achieve what I want but I have 105 records I want to iterate through, there has to be a better way to do this.
                    //place[u] = var place[u]
                    //place[1] = place[2]
                    //place[2] = place[3]
                    //if aPlace is equal to place0 then 0 = 1, next loop 1 = 2, next loop 2 =3
                    //you can't ++ a "String which place[with an index] is.

我一直在阅读这个 documentation并查看了许多不同的 stackoverflow 问题,但到目前为止没有任何帮助......我正在考虑一个 switch 语句,但不确定它是否会有所不同。

编辑:

for a in coor {
    for aPlace in place {
         for aPass in pass {


}}}

数据为 geojson 格式: 颜色:双(2343.90) 地方:字符串(“asdfsa”) pass: String ("asdfkkrr")

还有3条记录

我需要它按以下顺序在我的代码中进一步输入: coor1、place1 和 pass1,然后是 coor2、place2 和 pass2,等等。

最佳答案

在第一个 for 循环之外定义 u 变量,在外部循环结束时定义 u += 1

检查这个

var u : Int = 0
placeLoop: for aPlace in place {
             print("\(aPlace)")
             print(" ")

             if aPlace == place[u] {
             place[u] = place[++u]
             //This is the manual way to achieve what I want but I have 105 records I want to iterate through, there has to be a better way to do this.
             //place[u] = var place[u]
             //place[1] = place[2]
             //place[2] = place[3]
             //if aPlace is equal to place0 then 0 = 1, next loop 1 = 2, next loop 2 =3
             //you can't ++ a "String which place[with an index] is.
             u += 1

希望对你有帮助,谢谢

关于ios - 如何将变量设置为从 0 开始并通过嵌套 for 循环 Swift 2 中的每个循环获得一个级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38336064/

相关文章:

ios - 如何获取标注 c# xamarin 的 MKPlacemark 地址

ios - 更改图像图钉图

swift - 解决 Swift 3 中过多的 else-if 语句

c - 嵌套for循环逻辑

ios - Xcode UI 测试手册截图

ios - 为什么应用程序要签名两次才能分发?

ios - 删除函数数组中的索引时,编译器提示 'Expression resolved to unused function'

arrays - 在 Swift 中将字符串缩减为字典

ios - 快速,延迟加载的简短闭包形式

python - Itertools 相当于嵌套循环 "for x in xs: for y in ys..."