ฉันต้องการเพื่อสร้างทั่วไปประเภทที่ยอมรับอะไรนั่น conforms ต้อง CustomStringConvertible
และจากนั้น iterates นือพวกนั้นรายการ.
นี่คือเป็นตัวอย่างนั้น 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
ดังนั้นฉันทำอะไรผิดเหรอ?