ash4
ash4

Reputation: 101

Array and reversed giving same result but not equal

Im came across a situation which is difficult to understand. I have array of strings and then reversed but it prints the same array instead of reversing...

When compared, compiler says they are not equal

Below is the code...

Please help me understanding this phenomenon

let names = ["Andy", "Joy", "Paul"]
print(names) // Prints  ["Andy", "Joy", "Paul"]
print(names.reversed()) 
//Prints ReversedCollection<Array<String>>(_base: ["Andy", "Joy", "Paul"])`

//Prints Not equal
if names == names.reversed() {
    print("Equal")
} else {
    print("Not Equal")
}

I was expecting output to be ["Paul", "Joy", "Andy"] after

 names.reversed()

Upvotes: 0

Views: 244

Answers (4)

Sagar Bhut
Sagar Bhut

Reputation: 657

after writing

print(names.reversed()) 

//Prints ReversedCollection>(_base: ["Andy", "Joy", "Paul"])`

1). your array is reversed but that is not store in names so name is as it is at now.

2). when you reversed array at print time then that display ReversedCollection so that collection is reversed but not stored at now so that just manage internal for reverse array.

if you store in some object then that object print then that display reversed.

like:

let reversedName = names.reversed()
print(reversedName)

so it's print reversed array.

Upvotes: 0

Vicky_Vignesh
Vicky_Vignesh

Reputation: 592

As the Apple Document states

https://developer.apple.com/documentation/swift/array/1690025-reversed

You can reverse a collection without allocating new space for its elements by calling this reversed() method. A ReversedCollection instance wraps an underlying collection and provides access to its elements in reverse order. This example prints the characters of a string in reverse order:

let word = "Backwards"
for char in word.reversed() {
    print(char, terminator: "")
}
// Prints "sdrawkcaB"

If you need a reversed collection of the same type, you may be able to use the collection’s sequence-based or collection-based initializer. For example, to get the reversed version of a string, reverse its characters and initialize a new String instance from the result.

let reversedWord = String(word.reversed())
print(reversedWord)
// Prints "sdrawkcaB"

In your case you need to create a new instance like below

let reversedArray = names.reversed()
print(reversedArray) 
// Prints ["Paul", "Joy", "Andy"]

Upvotes: 0

inexcitus
inexcitus

Reputation: 2639

The result has not been processed yet (the ReversedCollection is lazy).

Please try mapping the results:

ReversedCollection

Upvotes: 0

rmaddy
rmaddy

Reputation: 318824

The call to reversed() doesn't modify the array. It returns a view to the original array.

When you call print(names.reversed()), it prints that reversed list but names itself is left unchanged.

The only time names == names.reversed() would be true is if the array was empty or it was a "palindrome" array.

Also keep in mind that you declared names as a constant (using let) so that should be a good hint that names hasn't been modified.

You could create a new array:

let reversedNames = names.reversed()

That new array will have the value in reversed order.

Upvotes: 2

Related Questions