#Warning: C-Style For Statement Is Deprecated and Will Be Removed in a Future Version of Swift

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

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
}

iOS Sample Image 24


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

Fix warning C-style for Statement is deprecated in Swift 3

C-style for loop has been deprecated in Swift 3. You can continue using it for a while, but they will certainly disappear in the future.

You can rewrite your loop to Swift's style:

for i in 0..    let length = UInt32 (letters.length)
let rand = arc4random_uniform(length)
randomString.appendFormat("%C", letters.characterAtIndex(Int(rand)))
}

Since you don't use i at all in the loop's body, you can replace it with:

for _ in 0..    // do stuffs
}

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

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.

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

Here how you rewrite your code with while cycle:

var viewController: UIViewController? {
var next: UIView? = self.superview
while next != nil {
let responder = next?.nextResponder()
if let vc = responder as? UIViewController {
return vc
}
next = next?.superview
}
return nil
}

C-style for statement has been removed in Swift 3 (Continuos error)

Keeping the index is easy with enumerated:

for (i, sender) in senderArray.enumerated() {
// Can simply use 'sender' here, no need to index into senderArray.
if sender == userName {
...

// Unchanged lines; 'i' is used as index here.
self.message2Array.append(self.messageArray[i])
self.sender2Array.append(self.senderArray[i])

}

What are the advantages Swift deprecates C-style for statement?

For details, see Swift Evolution - Remove C style for-loops

To quote the reasoning:

  1. Both for-in and stride provide equivalent behavior using Swift-coherent approaches without being tied to legacy terminology.
  2. There is a distinct expressive disadvantage in using for-loops compared to for-in in succinctness
  3. for-loop implementations do not lend themselves to use with collections and other core Swift types.
  4. The for-loop encourages use of unary incrementors and decrementors, which will be soon removed from the language.
  5. The semi-colon delimited declaration offers a steep learning curve from users arriving from non C-like languages
  6. If the for-loop did not exist, I doubt it would be considered for inclusion in Swift 3.

In summary: there are better ways (more expressive) than a C-style for-loop to iterate in Swift.

Some examples:

for-in over a range:

for i in 0 ..< 10 {
//iterate over 0..9
print("Index: \(i)")
}

for i in (0 ..< 10).reverse() {
//iterate over 9..0
print("Index: \(i)")
}

For arrays (and other sequences) we have many options (the following is not a complete list):

let array = ["item1", "item2", "item3"]

array.forEach {
// iterate over items
print("Item: \($0)")
}

array.reverse().forEach {
// iterate over items in reverse order
print("Item: \($0)")
}

array.enumerate().forEach {
// iterate over items with indices
print("Item: \($1) at index \($0)")
}

array.enumerate().reverse().forEach {
// iterate over items with indices in reverse order
print("Item: \($1) at index \($0)")
}

for index in array.indices {
// iterate using a list of indices
let item = array[index]
print("Item \(item) at index: \(index)")
}

Also note that if you are converting an array to another array, almost always you want to use array.filter or array.map or a combination of them.

For all Strideable types we can use the stride method to generate indices, for example:

for index in 10.stride(to: 30, by: 5) {
// 10, 15, 20, 25 (not 30)
print("Index: \(index)")
}

for index in 10.stride(through: 30, by: 5) {
// 10, 15, 20, 25, 30
print("Index: \(index)")
}

With arrays we can do:

for index in 0.stride(to: array.count, by: 2) {
// prints only every second item
let item = array[index]
print("Item \(item) at index: \(index)")
}

Code migration from Swift 2.x to Swift 4 (for loop)

Here's your statement in Swift 4:

for i in 1..
}

Replacement for C-style loop in Swift 2.2

Although it's not as "pretty", you can use stride:

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

++' is deprecated: it will be removed in Swift 3

Since Swift 2.2, you should use += 1 or -= 1 instead.

And after looking up Swift's evolution, there are some reasons for removing these operators:

  1. These operators increase the burden to learn Swift as a first programming language - or any other case where you don't already know these operators from a different language.

  2. Their expressive advantage is minimal - x++ is not much shorter than x += 1.

  3. Swift already deviates from C in that the =, += and other assignment-like operations returns Void (for a number of reasons). These operators are inconsistent with that model.

  4. Swift has powerful features that eliminate many of the common reasons you'd use ++i in a C-style for loop in other languages, so these are relatively infrequently used in well-written Swift code. These features include the for-in loop, ranges, enumerate, map, etc.

  5. Code that actually uses the result value of these operators is often confusing and subtle to a reader/maintainer of code. They encourage "overly tricky" code which may be cute, but difficult to understand.

  6. While Swift has well defined order of evaluation, any code that depended on it (like foo(++a, a++)) would be undesirable even if it was well-defined.

  7. These operators are applicable to relatively few types: integer and floating point scalars, and iterator-like concepts. They do not apply to complex numbers, matrices, etc.

Finally, these fail the metric of "if we didn't already have these, would we add them to Swift 3?"

Please check out Swift evolution for more info.



Related Topics



Leave a reply



Submit