弃用Swift C样式循环后循环中的递减索引,应该如何使用

Posted ShineYangGod

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了弃用Swift C样式循环后循环中的递减索引,应该如何使用相关的知识,希望对你有一定的参考价值。

方法一

for i in (0 ..< 5).reversed() 
    print(i) // 4,3,2,1,0


let array = ["a", "b", "c", "d", "e"]
for element in array.reversed() 
    print(element) // e,d,c,b,a


array.reversed().forEach  print($0)  // e,d,c,b,a

print(Array(array.reversed())) // e,d,c,b,a

方法二


for index in stride(from: 10, to: 0, by: -1) 
    print(index)


// You can also use stride condition like
// Some Value.stride(to: 0, by: -1)
// for index in 10.stride(to: 0, by: -1)  

以上是关于弃用Swift C样式循环后循环中的递减索引,应该如何使用的主要内容,如果未能解决你的问题,请参考以下文章