0

I have state data which is a collection of lists with key: values from json. I want to use ForEach View to show each list items in one list view but I'm getting Error. So I read that ForEach requires unique identifier, is there a hashable protocol for list like String and Int? Why am I getting this error?

Error:Cannot convert value of type 'Text' to closure result type '_'
struct PopUpTextView: View {
    @State private var data = [[String:Any]]()
    //let colors: [[String]] = [["red", "blue"], ["tan"],["green"], ["blue"]]

    func configureView() {
        takeRequest { (data, error) in
            print("before")
            print(data)
            self.data = data
            print("------")
            print(self.data)
        }
    }

    var body: some View {
        NavigationView{
            List{
                ForEach(self.$data, id: \.self){ c in
                    Text(c.description)
                }
            }.navigationBarTitle("Details")
        }.onAppear(perform:configureView)
    }
}
ilaunchpad
  • 1,283
  • 3
  • 16
  • 32
  • You don't make it work that way - create real struct model in array.... and that error is due to `self.$data` - it should be just `self.data`, but anyway, re-read from start. – Asperi Jun 09 '20 at 16:15
  • @Asperi If i do ``self.data`` I get ``Value of protocol type 'Any' cannot conform to 'Hashable'; only struct/enum/class types can conform to protocols``. But why does ForEach work for ``let colors: [[String]] = [["red", "blue"], ["tan"],["green"], ["blue"]]`` – ilaunchpad Jun 09 '20 at 19:07

2 Answers2

0

when dealing with array of dictionaries, I find it much easier to deal with the indices, such as:

            ForEach(self.data.indices) { index in
                Text("---> \(self.data[index].description)")
            }
0

Do you need the data variable of type String: Any?

Because if i use it like this:

@State private var data = [[String]]()

It works fine for me and no error occurs.

And of course like mention in the comments above remove the '$' in the foreach before data.

Alex B
  • 131
  • 7