#warning:不推荐使用C-style for语句,将在未来的Swift版本中删除 [英] #warning: C-style for statement is deprecated and will be removed in a future version of Swift

查看:342
本文介绍了#warning:不推荐使用C-style for语句,将在未来的Swift版本中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是使用swift 2.2下载一个新的Xcode(7.3)。

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

它有一个警告:


不推荐使用C-style for语句,将在未来的Swift版本中删除。

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

如何修复此警告?

推荐答案

删除 for init;比较;增加{} 并轻松删除 ++ - 。并使用Swift的漂亮for-in循环

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 index in 10.stride(to: 0, by: -1) {
      print(index)
  }

或者你可以使用 reverse()喜欢

  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:

来自 Swift3.0 ,Strideable上的 stride(to:by:)方法已替换为自由功能,步幅(从:到:由:)

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

对于 Swift 3.0 中的减量索引,您可以使用 reversed()

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

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

其他每个 stride(),你可以使用 While循环

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

重复循环:

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

签出控制流程 Swift编程语言指南

这篇关于#warning:不推荐使用C-style for语句,将在未来的Swift版本中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆