I have this loop:
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])")
}
I was expecting this output:
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
However, it seems that
nameIndex -= 1
Does not affect the actual variable. It temporarily reduces it, but as soon as the next iteration takes place the variable goes back to normal.
The actual output is:
*Her name is Sandra
Her name is Pedro
This name is to be repeated: John
Her name is Shay
Her name is Tyrion*
It is as if the nameIndex -= 1 did not affect anything.
How do I accomplish this? In other words, how do I accomplish iterating an item again in the next iteration.
PS: The example I posted above is a simplified way of describing my real application. It is for the sake of making this work:
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)
}
}
But I thought it'd be simpler to work off a simpler example.