#警告:C-style for statement已被弃用,将在Swift的未来版本[duplicate]中删除

时间:2022-09-30 22:28:46

This question already has an answer here:

这个问题已经有了答案:

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

我刚刚用swift 2.2下载了一个新的Xcode(7.3)。

It has a warning:

它有一个警告:

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

声明的c风格被弃用,将在未来版本的Swift中被删除。

How can I fix this warning?

我如何修正这个警告?

4 个解决方案

#1


72  

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

删除初始化;比较;增加{},并删除++和-容易。使用Swift的漂亮的forin循环

   // 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:

斯威夫特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:

斯威夫特3.0:

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

从Swift3.0开始,Strideable上的stride(to:by:)方法被一个*函数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()

对于Swift 3.0中的递减指数,可以使用reverse ()

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

#警告:C-style for statement已被弃用,将在Swift的未来版本[duplicate]中删除


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

对于each和stride(),您可以使用While循环

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

查看Swift编程语言指南中的控制流

#2


3  

For this kind "for" loop:

对于这种“For”循环:

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

You can write:

你可以写:

for i in (0...10).reverse() {
    print(i)
}

#3


1  

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):

当你试图用Xcode来修复它的时候,你不会有什么好运气……所以你需要使用新的swift样式(for in loop):

for i in 1...video.getAll().count {
    print("show number \(i)")
}

#4


0  

Blockquote

引用

Use this instead

使用这个代替

if(myarr.count)
{
    for i in 1...myarr?.count {
      print(" number is \(i)")
    }
}

#1


72  

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

删除初始化;比较;增加{},并删除++和-容易。使用Swift的漂亮的forin循环

   // 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:

斯威夫特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:

斯威夫特3.0:

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

从Swift3.0开始,Strideable上的stride(to:by:)方法被一个*函数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()

对于Swift 3.0中的递减指数,可以使用reverse ()

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

#警告:C-style for statement已被弃用,将在Swift的未来版本[duplicate]中删除


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

对于each和stride(),您可以使用While循环

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

查看Swift编程语言指南中的控制流

#2


3  

For this kind "for" loop:

对于这种“For”循环:

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

You can write:

你可以写:

for i in (0...10).reverse() {
    print(i)
}

#3


1  

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):

当你试图用Xcode来修复它的时候,你不会有什么好运气……所以你需要使用新的swift样式(for in loop):

for i in 1...video.getAll().count {
    print("show number \(i)")
}

#4


0  

Blockquote

引用

Use this instead

使用这个代替

if(myarr.count)
{
    for i in 1...myarr?.count {
      print(" number is \(i)")
    }
}