Swift 3:用浮点增量替换c样式for-loop [英] Swift 3: replace c style for-loop with float increment

查看:88
本文介绍了Swift 3:用浮点增量替换c样式for-loop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有这样一个循环:

I have such a loop in my app:

for var hue = minHue; hue <= maxHue; hue += hueIncrement
{
   let randomizedHue = UIColor.clipHue(
       Random.uniform(ClosedInterval(hue - dispersion, hue + dispersion))
   ) 
   colors.append(colorWithHue(randomizedHue))
}

hueIncrement float ,所以我不能使用这样的范围运算符: ..<

hueIncrement is float, so I cant use range operators like this: ..<.

在Swift 3中实现此类循环的最佳和最简洁的方法是什么?

推荐答案

你可以使用步幅函数 stride(通过:,by:)为此...类似

you can use stride function stride(through:, by:) for this .. something like

for hue in (minHue).stride(through: maxHue, by: hueIncrement){
    // ...
}

Swift3.0 ,您可以使用 stride(from:to:by:) stride(from:through:by:) syntax

From Swift3.0, you can use stride(from:to:by:) or stride(from:through:by:) syntax

for hue in stride(from: minHue, through: maxHue, by: hueIncrement){
    //....
}

这篇关于Swift 3:用浮点增量替换c样式for-loop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆