二元运算符 '..<' 不能应用于两个 'Int' 操作数 [Swift]

Binary operator '..<' cannot be applied to two 'Int' operands [Swift]

我在我的 Xcode 项目中编程,突然收到警告:

 for var i:CGFloat = 0; i<3; i++ 

"will be removed in Swift 3." 它给了我一个选项 "fix" Swift 3 语法的代码并将其切换为:

for i:CGFloat in 0 ..< 3 {

现在我的代码不会 运行 并且出现错误“二元运算符 '.<' 不能应用于两个 'Int' 操作数。

for i in 0..<3 {
    let someFloat = CGFloat(i) // if you need a CGFloat
}

检查间距,从循环声明中删除 CGFloat 类型。

如@JAL 所述,我认为您不能 运行 带有 CGFloatfor 循环,但您在这里也有几个选择。

您可以像这样使用 range 运算符:

// i is used an int
for i in 0..<3 {
    let convertedI = CGFloat(i) // This converts i to a new value as a CGFloat 
}

您也可以在 Swift 2 中使用 stride 并转换为 CGFloat:

// i is now a CGFloat 
for i in (0 as CGFloat).stride(to: 3, by: 1) {
    // No need to convert because i is now a CGFloat 
}

最后,strideSwift 3:

// i is an int in this scenario
for i in stride(from: 0, to: 3, by: 1) {
    let convertedI = CGFloat(i) // This converts i to a new value as a CGFloat 
}

更新:

更改了我的 Swift 2 stride 以将 i 转换为 CGFloat,感谢@Hamish。