使用可变变量

时间:2017-07-31 03:23:24

标签: ios swift sprite-kit

我有这个循环:

let names = ["Sandra", "Pedro", "John", "Shay", "Tyrion"]
var money = 0


for var nameIndex in 0..<names.count {
    if money > 30 {
        print("This name is to be repeated: \(names[nameIndex])")
        money = 0
        nameIndex -= 1
        continue
    }
    money += 25
    print("Her name is \(names[nameIndex])")
}

我期待这个输出:

Her name is Sandra
Her name is Pedro
This name is to be repeated: John
Her name is John
Her name is Shay
Her name is Tyrion

然而,似乎

nameIndex -= 1

不影响实际变量。它会暂时减少它,但是一旦下一次迭代发生,变量就会恢复正常。

实际输出是:

   *Her name is Sandra
   Her name is Pedro
   This name is to be repeated: John
   Her name is Shay
   Her name is Tyrion*

就好像nameIndex - = 1没有影响任何东西。

我如何做到这一点?换句话说,如何在下一次迭代中再次完成迭代项目。

PS:我上面发布的示例是描述我的实际应用程序的简化方法。这是为了使这项工作:

    for var word in 0..<rawText.count {
        let wordText = rawText[word]
        let wordLabel = SKLabelNode(fontNamed: "LCDSolid")
        wordLabel.text = wordText
        if scalingFactor == nil {
            scalingFactor = getScalingFactor(labelNode: wordLabel, size: adjustedSize)
        }
        wordLabel.fontSize *= scalingFactor
        let addedWidth = wordLabel.frame.width + self.size.width * CGFloat(0.03)
        currentWidth += addedWidth
        if currentWidth > desiredWidth {
            currentPos = (self.size.width * CGFloat(-0.475), currentPos.y - adjustedSize.height * CGFloat(1.5))
            currentWidth = 0
            word -= 1
            continue

        }
        wordLabel.position = CGPoint(x: currentPos.x + (wordLabel.frame.width * CGFloat(0.5)), y: currentPos.y)
        currentPos.x += addedWidth
        print("width is \(wordLabel.frame.width) and current width is \(currentWidth) and actual box width is \(self.size.width)")
        self.addChild(wordLabel)
    }
}

但我认为用更简单的例子来处理它会更简单。

4 个答案:

答案 0 :(得分:2)

我认为这就是你想要的:

let names = ["Sandra", "Pedro", "John", "Shay", "Tyrion"]
var money = 0
var nameIndex = 0
while nameIndex < names.count {

        if money > 30 {
                print("This name is to be repeated: \(names[nameIndex])")
                money = 0
                continue
        }
        money += 25
        print("Her name is \(names[nameIndex])")
        nameIndex = nameIndex + 1

}

您想要的输出:

Her name is Sandra
Her name is Pedro
This name is to be repeated: John
Her name is John
Her name is Shay
This name is to be repeated: Tyrion
Her name is Tyrion

答案 1 :(得分:1)

可能只需将if money > 30替换为while money > 30即可。不要真正理解你想要做什么,但它会做你所需要的,也不需要使用索引,只是正常for-in会做

let names = ["Sandra", "Pedro", "John", "Shay", "Tyrion"]
var money = 0

for var nameIndex in 0..<names.count {
    while money > 30 {
        print("This name is to be repeated: \(names[nameIndex])")
        money = 0
    }
    money += 25
    print("Her name is \(names[nameIndex])")
}

输出:

Her name is Sandra
Her name is Pedro
This name is to be repeated: John
Her name is John
Her name is Shay
This name is to be repeated: Tyrion
Her name is Tyrion

答案 2 :(得分:1)

发生这种情况的原因是因为Swift for循环遍历序列;也就是说,好像你写的那样:

for var nameIndex in [0, 1, 2, 3, 4, ... names.count - 1, names.count]

所以,第一次遍历循环,它将0分配给nameIndex,第二次分配给它,它分配1,依此类推。您对nameIndex所做的任何更改都将在循环的下一次迭代中重新分配给序列中的下一个元素时被删除。

你真正想要的是一个C for循环,但是因为那些被从Swift中删除了一段时间,你最好的选择可能就是使用变量和while循环:

var nameIndex = 0
while nameIndex < 0..<names.count {
    defer { nameIndex += 1 }
    // ...
}

答案 3 :(得分:1)

您需要修改for loop才能达到预期的输出效果。并采取Bool continueLoop 决定继续循环

    var money = 0
    let names = ["Sandra", "Pedro", "John", "Shay", "Tyrion"]
    var continueLoop = false 
    for nameIndex in 0..<names.count {
        if money > 30 {
            print("This name is to be repeated: \(names[nameIndex])")
            continueLoop = true
        }
        if continueLoop {
          money = 0
        } else {
            money += 25
        }
        print("Her name is \(names[nameIndex])")
    }

预期输出: -

  

她的名字是桑德拉

     

她的名字是Pedro

     

这个名字要重复:约翰

     

她的名字是约翰

     

她的名字是Shay

     

她的名字是提利昂