The following is my code for my simple contentView
struct ContentView: View {
@State private var selection = 1;
@State private var addFood = false;
var listItems = [
Food(name: "List Item One"),
Food(name: "List Item Two")
]
var body: some View {
TabView(selection: $selection) {
NavigationView {
List(listItems){
food in NavigationLink(destination: FoodView(selec: selection)) {
Text(food.name)
}
}.navigationBarTitle(Text("Fridge Items"), displayMode: .inline)
.navigationBarItems(trailing:
NavigationLink(destination: FoodView(selec: selection)) {
Image(systemName: "plus.circle").resizable().frame(width: 22, height: 22)
} )
}
.tabItem {
Image(systemName: "house.fill")
Text("Home")
}
.tag(1)
Text("random tab")
.font(.system(size: 30, weight: .bold, design: .rounded))
.tabItem {
Image(systemName: "bookmark.circle.fill")
Text("profile")
}
.tag(0)
}
}
}
struct FoodView: View{
var selec: Int?
var body: some View{
NavigationView{
Text("food destination view \(selec!)");
}
}
}
I follow some tutorials to get to this point. However, I'm confused about the syntax
List(listItems){
food in NavigationLink(destination: FoodView(selec: selection)) {
Text(food.name)
}
What does the above line mean? My guess is that we list out the items we have, and then for each food we have, we add a navigation link for them. However, that's my guess and I want to know the true syntax behind this. I've already read the documentation about List and also go through the official documentation about swift syntax. I didn't find anything useful. I thought it was a closure first, but I found there is still a difference. Could anyone help, please.