Decrement index in a loop after Swift C-style loops deprecated

前端 未结 5 1590
谎友^
谎友^ 2020-11-27 20:09

How would you express a decrementing indexed loop in Swift 3.0, where the syntax below is not valid any more?

for var index = 10 ; index > 0; index-=1{
           


        
5条回答
  •  失恋的感觉
    2020-11-27 20:28

    You can use stride method:

    10.stride(through: 0, by: -1).forEach { print($0) }
    

    or classic while loop.

提交回复
热议问题