0

How to reverse an integer array in swift

eg: var ab = [1,2,3,4]

I want to store the array in reverse format.

The result should be

ab = [4,3,2,1]
1
  • 3
    Please search before posting. Commented Mar 14, 2018 at 5:42

2 Answers 2

7

use ab.reversed() function which returns a reversed array.

Sign up to request clarification or add additional context in comments.

Comments

3
var ab = [1, 2, 3, 4]

func reverse(_ arr: [Int]) -> [Int] {
    return arr.reversed() // we return here
}
print(reverse(ab))  // The answer print as you need! [4, 3, 2, 1]  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.