Chcem vytvoriť všeobecný typ, ktorý akceptuje čokoľvek, čo vyhovuje CustomStringConvertible
a potom iterates cez tieto položky.
Tu je príklad, ktorý destiluje sa ustanovuje, že problém:
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))
Tak som sa zobrazí chybové hlásenie
Generic struct 'ForEach' requires that 'ItemType' conform to 'Hashable'
a
Generic parameter 'Hashable' could not be inferred
Takže to, čo robím zle?