Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Array Of Years in SwiftUI

I am using an array of years ranged 1950…2022.

This is my code to get this:

let years = (1950...2022).map() { String($0) }

But I have to go in descending order like 2022…1950, and if I write

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let years = (2022...1950).map() { String($0) }

This is giving me the following error:

Thread 1: Fatal error: Range requires lowerBound <= upperBound

Did anyone knows how to get it in descending order?

>Solution :

You should use reversed

(1950...2022).reversed().map({ String($0) }

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

Or you can use stride

stride(from: 2022, through: 1950, by: -1).map({ String($0) })

https://developer.apple.com/documentation/swift/1641347-stride

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading