What have I done wrong? I am trying to "List" a set of string values from an array, I spent all day trying to get it to work. What have I done wrong?
import SwiftUI
struct ContentView: View {
@State private var menuItems = ["Settings", "Readings Entry", "Food Diary"]
@State private var targets = ["Settings()", "ReadingsEntry()", "FoodDiary()"]
var body: some View {
NavigationStack {
List {
ForEach(menuItems) { menuLine in
print(menuLine)
}
}.navigationTitle("Menu")
}
}
}
The latest set of Error messages are:
Against the ForEach Line : Cannot convert value of type '[String]' to expected argument type 'Range<Int>'
Against the print line3: Type '()' cannot conform to 'View'
For the first error, you need to add an 'id' clause, like this:
ForEach(menuItems, id: \.self) { menuLine in
Here is an explanation of the reason for this (better than I could give):
https://www.hackingwithswift.com/books/ios-swiftui/why-does-self-work-for-foreach
For the second error, the ForEach statement needs to return a view, which print does not do. So you could include a Text within the ForEach, for example:
Text(menuLine)