1
let x = [1, 2, 2, 3, 3, 3, 1].reversed()
for element in x.method_name() {
    print(element)
}

This returns

Value of type 'ReversedCollection<[Int]>' has no member 'method_name'.

Why? How do I reference the method I have created and have it do the functions I need it to do?

However, if I use the below, the problem seems to disappear. I would just like to pass in an array and do let the function do all, i.e.:

let x = Array([1, 2, 2, 3, 3, 3, 1].reversed())
ItsPete
  • 2,363
  • 3
  • 27
  • 35
DDD
  • 21
  • 4
  • _Where_ have you created the method `method_name`? – Sweeper Dec 19 '19 at 00:25
  • So, I had created the method name seperately as an extension to an Array – DDD Dec 19 '19 at 00:30
  • `extension Array where Element:Equatable { func method_name() -> [Element] { total.append(element) } } }` – DDD Dec 19 '19 at 00:32
  • That method doesn't make much sense. What is `total`? What is `element`? Its signature says it returns an array, yet it doesn't. Note that you can [edit] your question. – Sweeper Dec 19 '19 at 00:34
  • Yes, its signature says it returns an array, but when I use the .reversed() on it, it seems to be converting the array into a reversed collection. – DDD Dec 19 '19 at 00:35
  • It was just a boilerplate method I had inserted. The method works well on just arrays, the real problem only occurs when I try to modify the array and pass the method on to it. – DDD Dec 19 '19 at 00:37
  • Can you please include full code to reproduce the problem - in the question? How do you define `method_name`? How do you modify array? – julka Dec 19 '19 at 01:36

1 Answers1

1

Just in case you don't fully understand the motivation behind this overload of reversed returning a ReversedCollection instead of an Array, a ReversedCollection is just a "reversed view" of your original array. It is not a reversed copy of the original array. This is to save time and space, like a "lazy" collection. See this post for more details.

This is why you need the Array(...) initialiser to turn the reversed collection back into an array. You are opting out of the laziness.

On the other hand, there is another overload of reversed that returns an Array directly. Normally this overload is not selected because it is defined in a less specific type - Sequence, as opposed to Array. You need to give enough information about the type to use this overload:

let x: [Int] = [1, 2, 2, 3, 3, 3, 1].reversed()
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Thank you for this. Incase, the list had strings or any other data type, how would I go about defining "x" to accept all these data types within the array? – DDD Dec 19 '19 at 01:14
  • Or instead can I ask my method to do this for me? A similar link is provided below LINK: [https://stackoverflow.com/questions/59382540/how-to-remove-only-adjacent-duplicates-in-an-array-of-integers-in-swift/59383218?noredirect=1#comment104983602_59383218] – DDD Dec 19 '19 at 01:23
  • @DDD You mean "ask your method to convert the reversed collection to an array for you"? Well yeah that would work, just write your method as an extension on `ReversedCollection` instead. But your method might violate the [SRP](https://en.wikipedia.org/wiki/Single_responsibility_principle). – Sweeper Dec 19 '19 at 01:31