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

How can I consolidate this Swift snippet?

My code smells. Please help improve this snippet in Swift5:

sectionItems = [[infos[0].infoText ?? "uhoh0"],  [infos[1].infoText ?? "uhoh0"],  [infos[2].infoText ?? "uhoh2"],  [infos[3].infoText ?? "uhoh3"],  [infos[4].infoText ?? "uhoh4"],  [infos[5].infoText ?? "uhoh5"],  [infos[6].infoText ?? "uhoh6"],  [infos[7].infoText ?? "uhoh7"],  [infos[8].infoText ?? "uhoh8"],  [infos[9].infoText ?? "uhoh9"]]

>Solution :

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

In your code you are essentially iterating over your infos array and then pulling out the infoText from it and then replacing it with "uhoh0" if it isn’t there.

And then placing that item into an array (which seems odd but…)

So…

let sectionItems = infos.map { [$0.infoText ?? "uhoh0"] }

Would do the same thing I think.

But I’d be interested to hear why you are doing this and what the rest of the code is.

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