美文网首页
swift for循环问题

swift for循环问题

作者: Fsn_soul | 来源:发表于2020-11-06 14:28 被阅读0次

大部分情况下系统的for-in可以满足我们的需求

for i in 0...10 { //正序
    print(i)
}

for i in 0..<10 {
    print(i)
}

for i in stride(from: 0, to: 10, by: 2) { //正序
    print(i)
}

for i in stride(from: 10, through: 0, by: -3) { //逆序
    print(i)
}

但是有一种,step递乘或递除的:

var n = 10
while n > 0 {
  print(n)
  n /= 2
}

要想转化为for-in写法,其实比较复杂了,真他妈搞不懂为啥要把C语言的写法给废除。
参考:Converting a C-style for loop that uses division for the step to Swift 3

相关文章

网友评论

      本文标题:swift for循环问题

      本文链接:https://www.haomeiwen.com/subject/cletbktx.html