I\'m wondering about the reversed() method on a swift Array:
var items = [\"a\", \"b\", \"c\"]
items = items.reversed()
the signature of the r
There are 3 overloads of reversed() for an Array in Swift 3:
By default, reversed()
pick the RandomAccessCollection's overload and return a ReversedRandomAccessCollection. However, when you write
items = items.reversed()
you are forcing the RHS to return a type convertible to the LHS ([String]
). Thus, only the 3rd overload that returns an array will be chosen.
That overload will copy the whole sequence (thus O(n)), so there is no problem overwriting the original array.
Instead of items = items.reversed()
, which creates a copy of the array, reverse that and copy it back, you could reach the same effect using the mutating function items.reverse(), which does the reversion in-place without copying the array twice.