Generic SwiftUI Component nu poate deduce Hashable pentru CustomStringConvertible

0

Problema

Vreau sa creez un tip generic, care acceptă nimic care să fie conformă cu CustomStringConvertible și apoi reiterează peste aceste elemente.

Aici este un exemplu care distilă în proporție de minimum jos problema asta:

public struct Test<ItemType: CustomStringConvertible, Hashable>: View {
    var items: [ItemType]

    public var body: some View {
        ForEach(items, id: \.self) { item in
            Text("test")
        }
    }

}
let items: [String] = ["a", "b"]
let viewController = UIHostingController(rootView: Test(items: items))

Așa că am obține o eroare Generic struct 'ForEach' requires that 'ItemType' conform to 'Hashable'

și Generic parameter 'Hashable' could not be inferred

Deci, ce fac gresit?

swiftui
2021-11-22 17:14:01
1

Cel mai bun răspuns

1

Ai problemă de sintaxă:

public struct Test<ItemType: CustomStringConvertible & Hashable>: View {   // <<: here!
    var items: [ItemType]

    public var body: some View {
        ForEach(items, id: \.self) { item in
            Text("test")
        }
    }

}
2021-11-22 17:20:18

În alte limbi

Această pagină este în alte limbi

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................