How to reverse array in Swift without using “.reverse()”?

后端 未结 21 1744
清歌不尽
清歌不尽 2020-12-08 20:14

I have array and need to reverse it without Array.reverse method, only with a for loop.

var names:[String] = [\"Apple\", \"Microsof         


        
21条回答
  •  Happy的楠姐
    2020-12-08 20:56

    You can use the swift3 document:

    let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
    let reversedNames = names.sorted(by: >)
    
    // reversedNames is equal to:
    //   ["Ewa", "Daniella", "Chris", "Barry", "Alex"]
    

提交回复
热议问题