Uniwersalny składnik SwiftUI nie może wyświetlić skrót do niestandardowego cabrio polecenia

0

Pytanie

Chcę stworzyć uniwersalny typ, który akceptuje wszystko, co odpowiada CustomStringConvertible a następnie powtarza te elementy.

Oto przykład, który analizuje ten problem:

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))

Dlatego pojawia się błąd: Generic struct 'ForEach' requires that 'ItemType' conform to 'Hashable'

i Generic parameter 'Hashable' could not be inferred

Więc co robię nie tak?

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

Najlepsza odpowiedź

1

Masz problem ze składnią:

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

W innych językach

Ta strona jest w innych językach

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