सामान्य SwiftUI घटक नहीं अनुमान Hashable के लिए CustomStringConvertible

0

सवाल

मैं बनाने के लिए चाहते हैं एक जेनेरिक प्रकार है कि स्वीकार करता है कि कुछ भी करने के लिए अनुरूप है CustomStringConvertible और फिर दोहराता उन पर आइटम नहीं है.

यहाँ एक उदाहरण है कि distils नीचे है कि समस्या:

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

तो मैं एक त्रुटि मिलती है Generic struct 'ForEach' requires that 'ItemType' conform to 'Hashable'

और Generic parameter 'Hashable' could not be inferred

तो क्या मैं गलत कर रहा हूँ?

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

सबसे अच्छा जवाब

1

आप वाक्य रचना मुद्दे:

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

अन्य भाषाओं में

यह पृष्ठ अन्य भाषाओं में है

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