gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:20:32 27 4
gpt4 key购买 nike

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

它有一个警告:

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

如何解决此警告?

最佳答案

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

   // 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 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:) 方法已替换为自由函数 stride(from :致:作者:)

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

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

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

enter image description here

<小时/>

除了foreachstride()之外,您还可以使用While循环

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

Repeat-While 循环:

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

查看 The Swift Programming Language Guide 中的控制流

关于ios - #警告: C-style for statement is deprecated and will be removed in a future version of Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37805660/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com