Let's say I have a struct:
var topMenu: [TopMenu] = [TopMenu(name: "Menu", index: 1),
TopMenu(name: "Search", index: 2),
TopMenu(name: "Profile", index: 3),
TopMenu(name: "Settings", index: 4)]
This struct contains a string and an index, so I can create a menu based on a HStack and a ForEach with this elements. This is fine. But what I'm trying to do now is the following:
Let's say this struct is dynamic: I can have 3 elements but I can also have 5/6 elements (based on a backend call). And I would like to render a different view for each of this element (on click). At the moment i'm doing this with a simple if based on our index:
if self.index == 1 {
First()
} else if self.index == 2 {
Second()
} else if self.index == 3 {
Third()
} else {
First()
}
But this is not the best approach if I have more elements etc ...
My thought are the following, but I don't know what is the best approach for this:
Create a funcion that returns
Any Viewbased on the index and give for each view a default name so I can iterate on? For example View1, View2 etc?Add something particular to my struct?
P.s. I would also like to do this in the safer way possible, I don't want crash etc! =)
Thank you!