3

Anyone came across this?

I ve got a nice little loop but it seems to get a warning.

 for(;;nimages++)
George Asda
  • 2,119
  • 2
  • 28
  • 54
  • 4
    "nice"? That loop would be bad code in every language I know. It works but it is not nice. – Sulthan Feb 15 '16 at 23:23
  • 3
    It was proposed and accepted to remove the ++ and -- operators from the Swift language in an upcoming release. For a full explanation you can reference the following link: https://github.com/apple/swift-evolution/blob/master/proposals/0004-remove-pre-post-inc-decrement.md C-Style loops will also be deprecated in the near future: https://github.com/apple/swift-evolution/blob/master/proposals/0007-remove-c-style-for-loops.md – Derek Lee Feb 15 '16 at 23:38
  • @DerekLee can you answer the question so I can accept your answer please? – George Asda Feb 15 '16 at 23:47

4 Answers4

3

It was proposed and accepted to remove the ++ and -- operators from the Swift language in an upcoming release, therefore the warning you're seeing is to help you avoid these types of constructs in your code before it is removed. (Please reference the link for a full explanation as well as the advantages and disadvantages that they provide.)

Please note that C-Style loops will also be deprecated in the near future according to the following proposal: https://github.com/apple/swift-evolution/blob/master/proposals/0007-remove-c-style-for-loops.md

Not knowing exactly what kind of logic you need to implement I don't feel confident recommending a solution, however in accordance with the above proposals I would recommend that you may want to become familiar with the Swift for-in and stride statements. Or, as another person recommended, using a while loop may also be appropriate.

Derek Lee
  • 3,452
  • 3
  • 30
  • 39
2

What's the question? The entire error message is

C-style for statement is deprecated and will be removed in a future version of Swift

You could replace this with something like

while true {
    // loop body
    nimages += 1
}

Finally, if you know the number of iterations you want, you can use a for-in loop:

for nimages in 0..<maxImages { /* loop body */ }
nielsbot
  • 15,922
  • 4
  • 48
  • 73
0

C Style For loops are deprecated and incrementing decrementing like these i++ , i-- also deprecated

Therefore couldn't be used this type of loops anymore

let myArray = ["one","two","three","four","five","six"]

for var i = 0; i < myArray.count; i++ {
    print(myArray[i])
}

Instead of use above syntax, we can use this

let myArray = ["one","two","three","four","five","six"]

for i in 0..<myArray.count {
    print(myArray[i])
}

And also this

for i in 0...myArray.count-1 {
    print(myArray[i])
}

If you are not familiar with Range Operators and Half-Open Range Operators this is the time (Document Link)

Range Operators

The closed range operator (a...b) defines a range that runs from a to b, and includes the values a and b. The value of a must not be greater than b.

Half-Open Range Operators

The half-open range operator (a..<b) defines a range that runs from a to b, but does not include b.

Alupotha
  • 9,710
  • 4
  • 47
  • 48
0

For index-- style

 for index in 10.stride(to: 0, by: -1) {
      print(index)//This is 10, 9, 8, ... 1 NOT 0
  }

For index++ style

for index in 0..<10 {
}
William Hu
  • 15,423
  • 11
  • 100
  • 121