Swift 2中的C样式循环 [英] C-style for loop in swift 2
本文介绍了Swift 2中的C样式循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
XCode告诉我以下for循环将无法编译.我知道C-Style循环在Swift 3中已被使用,但是我在该项目中使用了Swift 2,因此它应该可以工作.
XCode is telling me that the following for loop will not compile. I know that the C-Style Loop was depricated in Swift 3, but I am using Swift 2 on this project, so it should work.
for (incrementor = 0; incrementor < someArray.count; incrementor += 1) {
stuff in the for loop
}
推荐答案
您可以像这样简单地编写C样式的for循环
You can simply write a C-Style for loop like this
for var i = 1; i <= someArray.count; i += 1 {
print("i is equal to \(i)")
}
编写上面的代码时,您将看到类似以下的警告,但是正如您所说的那样,您只是在测试某些东西,所以您做对了.
You will see warning like below when you write the above code, but as you said you are just testing something so you are good to go.
这篇关于Swift 2中的C样式循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文