我想创建一个通用型的,就接受任何符合 CustomStringConvertible
然后重复这些项目。
这里是一个例子,蒸馏下问题:
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
所以我在做什么错的?