SwiftUI Listview With Children Error

I created ListView like this

struct LayerGroup: Identifiable {
  let id = UUID()
  var LayerId: Int
  var Visible: Bool
  var Active: Bool
  var Labelled: Bool
  var ProjectLayerGroupId: Int

}

struct MapLayerGroup :Identifiable{
  var id = UUID()
  var CanBeDeleted: Bool
  var ListOrder: Int
  var Mutual: Bool
  var Name: String
  var ProjectId: Int
  var ProjectLayerGroupId: Int
  var Xaml: String
  var LayerGroups : [LayerGroup]?
}
var body: some View {
        HStack{
//mapLayerGroups is collection of MapLayerGroup and populated
          List(mapLayerGroups, children: \.LayerGroups) { layer in
            Text(layer.Name)
          }
        }
}

This will return

Failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project

I do not see what is wrong with the object and list. Is there any that i missed ?

I use XCode 12.5

Answered by workingdogintokyo in 679846022

in your data structure you don't have a hierarchy of parent/children, you only have a flat array of MapLayerGroup each element containing an array of LayerGroup. So I guess the compiler cannot make sense of the non tree-structured data. Try a simple list in this case:

 List(mapLayerGroups, id: \.id) { layer in
      Text(layer.Name)
 }

If your intention was to have a tree structure hierachy, then you should have something like this:

struct MapLayerGroup: Hashable, Identifiable {
  var id = UUID()
  var children: [MapLayerGroup]? = nil  // <---
  var CanBeDeleted: Bool
  var ListOrder: Int
  var Mutual: Bool
  var Name: String
  var ProjectId: Int
  var ProjectLayerGroupId: Int
  var Xaml: String
  var LayerGroups: [LayerGroup]?
}
...

 List(mapLayerGroups, children: \.children) { layer in
      Text(layer.Name)
  }


See also the Apple doc (halfway down the page) at:

Apple Documentation

Accepted Answer

in your data structure you don't have a hierarchy of parent/children, you only have a flat array of MapLayerGroup each element containing an array of LayerGroup. So I guess the compiler cannot make sense of the non tree-structured data. Try a simple list in this case:

 List(mapLayerGroups, id: \.id) { layer in
      Text(layer.Name)
 }

If your intention was to have a tree structure hierachy, then you should have something like this:

struct MapLayerGroup: Hashable, Identifiable {
  var id = UUID()
  var children: [MapLayerGroup]? = nil  // <---
  var CanBeDeleted: Bool
  var ListOrder: Int
  var Mutual: Bool
  var Name: String
  var ProjectId: Int
  var ProjectLayerGroupId: Int
  var Xaml: String
  var LayerGroups: [LayerGroup]?
}
...

 List(mapLayerGroups, children: \.children) { layer in
      Text(layer.Name)
  }


See also the Apple doc (halfway down the page) at:

Apple Documentation

SwiftUI Listview With Children Error
 
 
Q