ARTICLE AD BOX
ForEach requires its data parameter to conform to RandomAccessCollection. Dictionary ([Key: Value]) itself does not conform to RandomAccessCollection. Its keys and values views are collections, but the dictionary as a whole isnt directly usable in ForEach. Converting to an array (like Array(dict.values) or Array(dict)) gives ForEach the collection type it needs.
import SwiftUI struct NodeGroup: Identifiable { let id = UUID() var groupId: String var groupName: String } struct NodeNetwork { let id = UUID() var nodeGroups: [String: NodeGroup] = [:] } func loadNetwork() -> NodeNetwork { var network = NodeNetwork() network.nodeGroups["A"] = NodeGroup(groupId: "A", groupName: "Alpha") network.nodeGroups["B"] = NodeGroup(groupId: "B", groupName: "Beta") return network } struct ContentView: View { let networkModel = loadNetwork() var body: some View { let groups = Array(networkModel.nodeGroups.values) List { ForEach(groups) { group in Text(group.groupName) } } } }You need to iterate the tuples using id: \.key
struct ContentView: View { let networkModel = loadNetwork() var body: some View { let entries = Array(networkModel.nodeGroups) // [(key: String, value: NodeGroup)] List { ForEach(entries, id: \.key) { entry in HStack { Text(entry.key) Text(entry.value.groupName) } } } } }then you should lookup by iterating the sorted keys
struct ContentView: View { let networkModel = loadNetwork() var body: some View { let keys = networkModel.nodeGroups.keys.sorted() List { ForEach(keys, id: \.self) { key in if let group = networkModel.nodeGroups[key] { Text("\(key): \(group.groupName)") } } } } }Kindly ensure ForEach is inside a container (List, VStack, etc.). Using ForEach directly in body without a container can lead to layout issues. Also, please avoid calling loadNetwork() inside body rather use a stored property or @StateObject/@ObservedObject if the data changes.
