0

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.

1
  • let names = ["Sandra", "Pedro", "John", "Shay", "Tyrion"] var money = 0 ??? Commented Jul 31, 2017 at 3:24

4 Answers 4

2

I think this is what you want :

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

}

Your desired output:

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
Sign up to request clarification or add additional context in comments.

Comments

1

Probably just replace your if money > 30 with while money > 30 will do it. Don't really understand what you are trying to do, but it will do what you required, also dont need use index, just normal for-in will do

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])")
}

Output:

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

Comments

1

The reason that's happening is because Swift for loops iterate over a sequence; i.e., it's as if you'd written:

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

So, the first run through the loop, it assigns 0 to nameIndex, the second time through, it assigns 1, and so on. Any changes you make to nameIndex will be wiped out when it reassigns it to the next element in the sequence on the next iteration of the loop.

What you really want here is a C for loop, but since those were removed from Swift a while back, your best bet is probably just to use a variable and a while loop:

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

Comments

1

You need to modify your for loop in order to achieve your expected output. and take Bool continueLoop which decide to continue loop or not

    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])")
    }

Expected 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.