Swift .reverse()

Christine_Yang's avatar
Published Oct 11, 2022
Contribute to Docs

The .reverse() method reverses an array in place, mutating the original array, and returns the elements within it so that the first element becomes the last element and vice versa.

  • Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.
    • Includes 7 Courses
    • With Certificate
    • Beginner Friendly.
      13 hours
  • A powerful programming language developed by Apple for iOS, macOS, and more.
    • Beginner Friendly.
      12 hours

Syntax

arrayName.reverse()

Example

var topBabyNames = ["Jackson", "Liam", "Noah", "Olivia", "Riley", "Sophia"]
topBabyNames.reverse()
print(topBabyNames)
// Output: ["Sophia", "Riley", "Olivia", "Noah", "Liam", "Jackson"]

All contributors

Contribute to Docs

Learn Swift on Codecademy

  • Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.
    • Includes 7 Courses
    • With Certificate
    • Beginner Friendly.
      13 hours
  • A powerful programming language developed by Apple for iOS, macOS, and more.
    • Beginner Friendly.
      12 hours