How to resolve the error "Initializer 'init(_:id:rowContent:)' requires that '(String, String)' conform to 'Hashable'" while using Lists in SwiftUI

Viewed 4699

Below is the code:

struct DetailView: View {
let trafficRule: [(String, String)] = [("123","rule1"),("123","rule2")]
var body: some View {
List(trafficRule, id: \.self) { trafficRule in
            Text("\(trafficRule.0)")
            Spacer()
            Text("\(trafficRule.1)")
}
}

Error message is showing up for the List block.

1 Answers

Tuples do not conform to Hashable in swift, and for that reason they cannot be used as the elements of a List. Instead of using tuples, consider making a struct to represent your elements, and conform it to Hashable:

struct TrafficRule: Hashable {
    let name: String
    let rule: String
}
Related