How can I consolidate this Swift snippet? [closed]
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"]]
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
.enumerated()
.map { (index, o) in [o.infoText ?? "uhoh\(index)"] }
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.