Hi @sprouse5,
if you write code, you can basically do whatever you want, so the short answer is yes, you can so that.
However it is a bit unusual to flatten all the elements.
update the code below into your code let strings = categories.flatMap({ [$0.title, $0.icons.first!, $0.icons.last!] })
However this relies on the fact that you have exactly 2 elements in the icons array
Thanks for your reply! I’m flattening all the strings that I’m reading in from a plist file to get a single string array of categories for use in filtering later on in the app. My solution was to create a computed property like this:
struct Category {
let title: String
let icons: [String]
var names: [String] {
var strings = icons
strings.insert(title, at: 0)
return strings
}
}
I create a copy of the icons array, add the title to the beginning of it, and return it. That way I can flat map like so:
let data = categories.flatMap { $0.names }
If there’s a better/more efficient way to do this I’d like to know, as I’m trying to create this app using iOS best practices. Thanks for your time!