<分区>
我刚刚下载了一个带有 swift 2.2 的新 Xcode (7.3)。
它有一个警告:
C-style for statement is deprecated and will be removed in a future version of Swift.
如何修复此警告?
<分区>
我刚刚下载了一个带有 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() { ... }
对于float类型(不需要定义任何类型来索引)
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 :to:by:)
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
}
除了for each
和stride()
,你还可以使用While Loops
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/36173379/
相关文章:
ios - Xcode 6.0.1 Instruments iOS8 - 在设备上进行分析时应用程序表现不正常
swift - 抑制警告: The Swift file cannot be processed by a Copy Bundle Resources build phase
swift - 什么是 Java 的比较器接口(interface)的 Swift 等价物
ios - 从另一个 vc 重新加载 vc TableView 支持的最佳方法
ios - UITableViewController 的子类和变量初始化。 iOS、环球银行金融电信协会
ios - 在 xib 文件中加载 View Controller