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?