Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

swift
reverse
communitycreator

What is the reversed() method in Swift?

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The reversed() method in Swift returns a view that presents the elements of a collection in the reverse order.

Syntax

sequence.reversed()

Parameters

This method does not take any parameters.

Return value

The value returned is a view showing the elements of the sequence sequence in the reverse order.

Code example

// create some sequences
let ages = [34, 23, 46, 26]
let name = "Theodore"
// get reverse view
print(Array(ages.reversed()))
print(String(name.reversed()))
Get the reverse view of a sequence in Swift

Code explanation

  • Lines 2–3: We create two sequences.
  • Lines 6–7: We invoke the reversed() method on the sequences that we created. Then, we print the resulting view to the console.

RELATED TAGS

swift
reverse
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring