Can't ForEach an array in a Dictionary

I have an array of "Groups" in a dictionary defined with [String: [AnyObject]]
I am trying to do a ForEach with this array in a SwiftUI View but getting: Cannot convert value of type 'String' to expected argument type 'Range<Int>' and Referencing initializer 'init(_:content:)' on 'ForEach' requires that '[String : [AnyObject]]' conform to 'Identifiable'
My code is:
Code Block
var body: some View {
NavigationView {
List {
ForEach(user.groups["groups"]) { group in
}
}
}
}

Please show more context. I cannot reproduce the same errors with your code.
Please do not forget to include whole definition of your user. And please clarify, are you using Xcode 12 beta?
Some other points

user.groups["groups"] is an optional. Cannot be used as is, need to unwrap

Should probably add , id: \.self in ForEach

The following runs oK:

Code Block
struct ContentView: View {
    var users : [String: [AnyObject]] = ["groups" : ["1", "2"] as [AnyObject]]
var body: some View {
NavigationView {
List {
ForEach(users["groups"]! as! [String], id: \.self) { group in
Text("Hello \(group)")
}
}
}
}
}

The values passed to ForEach need to have an ID. There are two ways to achieve this:
  1. Make the elements of the Arrays in your Dictionary be of a type conforming to Identifiable. (AnyObject doesn’t.)

  2. Provide a KeyPath to the id: parameter which specifies how to retrieve the ID. (You won’t be able to get a KeyPath from AnyObject.)

You’ve probably noticed a common theme: AnyObject is not going to work for this. You need to specify a more specific type and then make sure ForEach knows how to get an ID from that type.
Can't ForEach an array in a Dictionary
 
 
Q