LoginSignup
2

More than 5 years have passed since last update.

Rewriting typical C-Style for statements toward Swift 3.0

Last updated at Posted at 2016-04-11

C-Style for statement has been deprecated.

As you know C-Style for statement has been deprecated, and will be removed in Swift 3.0. Then I wrote an article about how to rewrite your C-Style for statements into new Swift 3.0 ready style, based on typical scenarios.

Screen Shot 2016-04-08 at 11.55.56 AM.png

Note: This article is an English translated version of this article.

for var i = 0 ; i < 100 ; i++

This scenario is the most typical for statement from 0 to 99.

// C-Style for statement
for var i = 0 ; i < 100 ; i++ {
    print("\(i)")
}
// Swift 3.0 ready
(0 ..< 100).forEach { print("\($0)") }
// Swift 3.0 ready
for i in (0 ..< 100) {
    print("\(i)")
}

for var i = 99 ; i >= 0 ; i--

Typical downward for statement from 99 to 0.

// C-Style for statement
for var i = 99 ; i >= 0 ; i-- {
    print("\(i)")
}

You ay use reverse().

// Swift 3.0 ready
(0 ..< 100).reverse().forEach { print("\($0)") }
// Swift 3.0 ready
for i in (0 ..< 100).reverse() {
    print("\(i)")
}

for var i = 0 ; i < 100 ; i += 2

Case where it's incrementation is more then one.

// C-Style for statement
for var i = 0; i < 100 ; i += 2 {
    print("\(i)")
}

You may use stride(). It has a parameter not only to but also through.

// Swift 3.0 ready
0.stride(to: 100, by: 2).forEach { print("\($0)") }

Difference between stride(to:by:) an stride(through:by:)

Note that to refers to "less than", and through refers to "equal or less than". Please see these examples.

0.stride(to: 20, by: 5) // 0, 5, 10, 15
0.stride(through: 20, by: 5) // 0, 5, 10, 15, 20

for var i = 98 ; i >= 0 ; i -= 2

Case when decrementing.

// C-Style for statement
for var i = 98 ; i >= 0 ; i -= 2 {
    print("\(i)")
}
// Swift 3.0 ready
98.stride(through: 0, by: -2).forEach { print("\($0)") }
// Swift 3.0 ready
0.stride(to: 100, by: 2).reverse().forEach { print("\($0)") }
// Swift 3.0 ready
for i in 0.stride(to: 100, by: 2).reverse() {
    print("\(i)")
}

When incrementation or decrementation is given in body code, and it's varying depending on condition.

I cannot come up with good example, but say, incrementation is depending on it's squared number is whether odd or even. (This example is totally a fictional)

// C-Style for statement
for var i = 0 ; i < 100 ; {
    print("\(i)")
    if (i * i) % 2 == 0 { i += 1 }
    else { i += 2 }
}

You may rewrite this with while statement.

// Swift 3.0 ready
var i = 0
while i < 100 {
    print("\(i)")
    if (i * i) % 2 == 0 { i += 1 }
    else { i += 2 }
}

When there are some cases to leave for statement.

I cannot come up with good example once again, but say, there is a case when you generate a random number and leave for statement depending on the generated number.

// C-Style for statement
for var i = 0 ; i < 100 ; i++ {
    print("\(i)")
    if arc4random_uniform(10) == 0 { break }
}

You may not use forEach {} to break for statement, so you may use for-in statement.

// error: forEach で break は使えない
(0 ..< 100).forEach {
    print("\(i)")
    if arc4random_uniform(10) == 0 { break } // error
}
// Swift 3.0 ready
for i in 0 ..< 100 {
    print("\(i)")
    if arc4random_uniform(10) == 0 { break } // OK
}

If your code has continue statement, you may not use forEach statement, so as break statement. So please rewrite that code with for-in statement as well.

Floating number, Not Integer

You may use stride() for CGFloat and Double because they are Strideable as well.

CGFloat(0).stride(to: 100, by: 1.5).forEach { print("\($0)") }
0.0.stride(to: 100.0, by: 1.5).forEach { print("\($0)") }

The last word

When Apple announces Swift 3.0, Apple may state something like "We provide best converter you can count on." Well, I am sorry, I don't think I would trust. Instead, Apple gave us some time to rewrite part of our code bit by bit, and this is much better way to be part of the Swift revolution.

Then Swift programmers!! Have a happy coding!!

Swift Version:
Apple Swift version 2.2 (swiftlang-703.0.18.1 clang-703.0.29)

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2