How to load an array of tuples in a SwiftUI List

The following code produces the error Type cannot conform to 'Hashable'.

How can I load an array of tuples to a SwiftUI List?


import SwiftUI
struct TestingGeneral: View {

    let users: [(name: String, age: Double)] = [
        ("Jeff", 25),
        ("Nathan", 18)
    var body: some View {
            List {
                ForEach(users, id: \.self) { user in


Type ‘(name: String, age: Double)’ cannot conform to ‘Hashable’

>Solution :

Using self in a ForEach is not very compatible with SwiftUI

ForEach(users, id: \.self) { user in

You should always have a unique variable. SwiftUI is all about identity.

ForEach(users, id: \.name) { user in

Check out Demystify SwiftUI from #wwdc21

Leave a Reply