|
Hello community, I'm new here and I'm glad for having initiated this course and grateful for Paul making it available for free. I've encountered a problem with the .reversed() function on arrays, which is that I don't know how to actually use them. Even if they are not really arrays anymore, I expected to be able to access any index of them as if they were. Otherwise, what's the point? Thanks! Lucas |
|
Can you show some code that illustrates your confusion? What is it you are expecting to be able to do that you can't do? |
|
Maybe these articles will help. Loop though an array in reverse
To create a new item with reversed elements you actually need to specify the element type - which is how Try the code in the
|
|
Thank you for your answers, these were helpful. At least now I know that with type annotation The thing that was causing confusion to me was this: In the
|
|
Using Being a Apple Documentation for ReversedCollection You can also see the same documentation in Xcode. Try adding the following code to the end of the previous code in the Playground.
The results using
Until the Strictly speaking, this also allocates memory, but it is released as soon as the print function is completed.
|
|
I think I know how to answer Lucas's question The code that Paul typed only want to show this message: ReversedCollection<Array<String>>(_base: ["Bush", "Obama", "Trump", "Biden"]) in english: this is the reversed collection of string array that take from the base of "Bush" "Obama" "Trump" "Biden" there is nothing useful about this because he only wanted to show you that message, how clever swift is. He wanted to show that using reversed() can save a lot time and memory. Instead of you creating new array that going to take more memory, the new reversed array will take the original array and remember them and reverse them internally. hope I don't disappoint you with my broken English answer. |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.