swiftswiftuiswift5

SwiftUI build a list using enums


I am trying to create SwiftUI List based on enum strings. I am faced with this error:

Cannot invoke initializer for type 'List<_, _>' with an argument list of type '([HomeView.Data], @escaping (String) -> HomeMenuRow)'

enter image description here

I can't understand how to use id or how to iterate through enum to build a row.


Solution

  • try this:

    enum Whatever  : String, CaseIterable, Identifiable {
    
        var id : String { UUID().uuidString }
    
        case one = "one"
        case two = "two"
    }
    
    struct ContentView: View {
        var body: some View {
            VStack {
                List (Whatever.allCases) { value in
                    Text(value.rawValue)
                }
            }
        }
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }