0

I have a converted for-loop in Swift 3.2 that looks similar to this:

for var i in 0..<char.characters.count {
    if(self.characters.count > len  && ((currentIndex + length2323) < length))
    {
      i = i - 1
    }
}

But, It doesn't work properly. I want to continue loop when set value for i is i = i - 1 but this code getting out of loop

And my previous Swift 2 code is :

for(var i = 0 ; i < char.characters.count ; i += 1) {
     if(self.characters.count > len  && ((currentIndex + length2323) < length))
     {
        i = i - 1
     }
}
New-Learner
  • 229
  • 1
  • 3
  • 15
  • You can always convert a C-style for loop to a while loop like [this](https://stackoverflow.com/a/42662061/1630618) – vacawama Jan 31 '18 at 12:04
  • 2
    Btw, can you explain what `if` inside loop does? – user28434'mstep Jan 31 '18 at 12:11
  • please try my code bellow, it would be great if you post all your code with cntext and what problem it should solve... – swift2geek Jan 31 '18 at 12:15
  • 2
    Is `i` used for something that's not included in this code? In the code you've included no variables are modified and `i` is only available inside the loop body (and isn't read or passed anywhere). – David Rönnqvist Jan 31 '18 at 12:21

2 Answers2

0

Swift 4 syntax

import UIKit
var char = "char"
var len = 9
var currentIndex = 1
var length2323 = 2323
var length = 17
for var i in 0..<char.count {
if (self.count > len)  && ((currentIndex + length2323) < length) {
    i = i - 1
}

}

Swift 3.2 syntax

import UIKit
var char = "char"
var len = 9
var currentIndex = 1
var length2323 = 2323
var length = 17
for var i in 0..<char.characters.count {
if (self.characters.count > len)  && ((currentIndex + length2323) < length) {
    i = i - 1
}

}

swift2geek
  • 1,697
  • 1
  • 20
  • 27
0
for (index, item) in char.enumerated()
{
      //your loop
}
Krishna Kirana
  • 438
  • 4
  • 10