0

I am trying to iterate though a array of through dictionaries in SwiftUI but I am getting the following error:

Cannot convert value of type 'PostViewModel' to expected argument type 'Range'

ForEach(self.posts) { post in
    dump(post)
}

What I want is to create run a loop that creates a list for each group and then populate each list with the post data.

Can someone help me out?


The data looks like this:

[
  "group1": [
    Tabs.Post(
      data: Tabs.Data(
        title: "Nulla vitae elit libero, a pharetra augue",
        id: "g4qq0o"
      )
    ),
    Tabs.Post(
      data: Tabs.Data(
        title: "Aenean lacinia bibendum nulla sed consectetur",
        id: "g76f3c"
      )
    ),
    Tabs.Post(
      data: Tabs.Data(
        title: "Fusce dapibus, tellus ac cursus commodo",
        id: "g7ftg9"
      )
    )
  ], 
  "group2": [
    Tabs.Post(
      data: Tabs.Data(
        title: "Nulla vitae elit libero, a pharetra augue",
        id: "498wi5"
      )
    ),
    Tabs.Post(
      data: Tabs.Data(
        title: "Aenean lacinia bibendum nulla sed consectetur",
        id: "g6x11c"
      )
    ),
    Tabs.Post(
      data: Tabs.Data(
        title: "Fusce dapibus, tellus ac cursus commodo",
        id: "g64l3b"
      )
    )
  ]
]
Dachan
  • 157
  • 4
  • 16
  • you can't iterate through dictionaries on swiftUI for more information you can go to this question https://stackoverflow.com/questions/56675532/swiftui-iterating-through-dictionary-with-foreach – Moumen Alisawe Apr 25 '20 at 02:26
  • So then how would I structure the data? would I need to put each group in its own array and then have an array that acts as an index? – Dachan Apr 25 '20 at 03:38
  • maybe you can use nested array or a separated array for each type, i hope that my answer is useful for you. – Moumen Alisawe Apr 25 '20 at 04:04

0 Answers0