George Asda
George Asda

Reputation: 2129

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

Anyone came across this?

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

 for(;;nimages++)

Upvotes: 3

Views: 2443

Answers (4)

William Hu
William Hu

Reputation: 16149

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 {
}

Upvotes: 0

Alupotha
Alupotha

Reputation: 10103

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.

Upvotes: 0

nielsbot
nielsbot

Reputation: 16032

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 */ }

Upvotes: 2

Derek Lee
Derek Lee

Reputation: 3670

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.

Upvotes: 3

Related Questions