Swift how to iterate float

主宰稳场 提交于 2019-12-25 08:15:05

问题


I tried to find this in Swift 2 docs, but can't find any info how to iterate floats. New swift iteration style make me crazy =) with dots and etc.

for (i = 0; i < 100; i+=0.5)

how to write it in Swift?

This does not work as it's Double:

for _ in 5.0.stride(100.0, by: 0.5) {
}

回答1:


This is the (exact) equivalent of your C-loop using stride

Swift 2

for index in 0.0.stride(to: 99.5, by: 0.5) {
  print(index)
}

Swift 3

for index in stride(from:0.0, through: 99.5, by: 0.5) {
  print(index)
}


来源:https://stackoverflow.com/questions/39774024/swift-how-to-iterate-float

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!