本文介绍了Swift 2.2 CGFloat中的C样式循环的替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经使用CGFloat尝试了此操作,但出现以下错误:无法使用类型为'((CGFloat by:CGFloat)')的参数列表来调用步幅
I have tried this with CGFloat and i am getting the following error: Can not invoke stride with an argument list of type '(CGFloat by: CGFloat)'
for var min:CGFloat = 0.0; min<=45.0; min = min+value {
print("\(min)")
}
收件人:
for min:CGFloat in 0.stride(CGFloat(55.0), by: min+value) {
print("\(min)")
}
推荐答案
以下是跨步的最新重载.您可以将数字强制转换为CGFloat.
Below is the latest overload for stride. You can use cast the number to CGFloat for the stride.
for min in (0 as CGFloat).stride(to: 55, by: value) {
print("\(min)")
}
但是,在for循环开始时,跨步会返回Striable. by
值不会随着for循环的迭代而更新.在这种情况下,使用while循环会更好,
However, stride returns a Striable when the for-loop begin. The by
value does not update with the iteration of for-loop. A while-loop would be better for this case,
var min : CGFloat = 0
while (min < 55) {
print("\(min)")
min += value
}
这篇关于Swift 2.2 CGFloat中的C样式循环的替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!