31

I just download a new Xcode (7.3) with swift 2.2.

It has a warning:

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

How can I fix this warning?

Hawken MacKay Rives
  • 1,171
  • 1
  • 16
  • 26
sony
  • 730
  • 2
  • 9
  • 19
  • 1
    dont use c-style for loops to fix... :P since its basically saying your code *will* break in the future, so why delay the inevitable. use `for i in 0...10 { }` instead – Fonix Mar 23 '16 at 08:46
  • Possible duplicate of http://stackoverflow.com/questions/36213333/swift-3-c-style-for-statement-is-deprecated – ivan_pozdeev Apr 04 '16 at 13:24
  • 1
    Not quite a duplicate of [Replacement for C-style loop in Swift 2.2](http://stackoverflow.com/questions/36166907/replacement-for-c-style-loop-in-swift-2-2) - that is for special cases where the new style cannot accurately replicate the functionality. – ivan_pozdeev Apr 04 '16 at 13:28

4 Answers4

81

Removing for init; comparison; increment {} and also remove ++ and -- easily. and use Swift's pretty for-in loop

   // WARNING: C-style for statement is deprecated and will be removed in a future version of Swift
   for var i = 1; i <= 10; i += 1 {
      print("I'm number \(i)")
   }

Swift 2.2:

   // new swift style works well
   for i in 1...10 {
      print("I'm number \(i)")
   }  

For decrement index

  for index in 10.stride(to: 0, by: -1) {
      print(index)
  }

Or you can use reverse() like

  for index in (0 ..< 10).reverse() { ... }

for float type (there is no need to define any types to index)

 for index in 0.stride(to: 0.6, by: 0.1) {
     print(index)  //0.0 ,0.1, 0.2,0.3,0.4,0.5
 }  

Swift 3.0:

From Swift3.0, The stride(to:by:) method on Strideable has been replaced with a free function, stride(from:to:by:)

for i in stride(from: 0, to: 10, by: 1){
    print(i)
}

For decrement index in Swift 3.0, you can use reversed()

for i in (0 ..< 5).reversed() {
    print(i) // 4,3,2,1,0
}

enter image description here


Other then for each and stride(), you can use While Loops

var i = 0
while i < 10 {
    i += 1
    print(i)
}

Repeat-While Loop:

var a = 0
repeat {
   a += 1
   print(a)
} while a < 10

check out Control flows in The Swift Programming Language Guide

Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
  • This style we can but if we want to loop from array and if that array have zero value it happen crash – sony Mar 23 '16 at 08:53
  • 2
    @sony: See [this Q&A](http://stackoverflow.com/a/36167115/4573247) for alternatives to the C-style loop, which allows for arrays of zero length (without needing additional if statements). – dfrib Mar 23 '16 at 09:24
  • yes got it ... but it gives you solution `i in – Bhavin Bhadani Mar 23 '16 at 09:27
  • 1
    @ElCaptain: Yes there's a few ways to circumvent looping over the members of an array that might be zero, the `max(...)` solution, or `.forEach{ ... }`, or `from.stride(to:by)` etc. Happy to help. – dfrib Mar 23 '16 at 09:31
  • @EICaptain: How to use this with CGFloat? Until now it was possible to set a type for the index. – Ich Mar 23 '16 at 15:57
4

For this kind "for" loop:

for var i = 10; i >= 0; --i {
   print(i)
}

You can write:

for i in (0...10).reverse() {
    print(i)
}
coldfire
  • 946
  • 10
  • 8
2

I got the same error with this code:

for (var i = 1; i != video.getAll().count; i++) {
    print("show number \(i)")
}

When you try to fix it with Xcode you get no luck... So you need to use the new swift style (for in loop):

for i in 1...video.getAll().count {
    print("show number \(i)")
}
SDW
  • 1,880
  • 4
  • 19
  • 30
0

Blockquote

Use this instead

if(myarr.count)
{
    for i in 1...myarr?.count {
      print(" number is \(i)")
    }
}
Moin Shirazi
  • 4,372
  • 2
  • 26
  • 38