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

前端 未结 5 1591
谎友^
谎友^ 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:29

    If you still want to use this C-style loop, here is what you need:

    let x = 10
    
    infix operator ..> { associativity left }
    func ..>(left: Int, right: Int) -> StrideTo {
        return stride(from: left, to: right, by: -1)
    }
    
    for i in x..>0 {
        print(i)
    }
    

提交回复
热议问题