SwiftUI - 视图显示符合协议的元素和 ForEach 在它们之上
SwiftUI - View showing Elements conforming to a Protocol and ForEach over them
我想编写一个 SwiftUI 视图 ListOfMyStruct,它在符合给定协议 MyProtocol.[=21 的元素数组上运行=]
该示例有效,但我当然需要 ForEach 遍历数组的元素(如在注释掉的行中尝试的那样)。
使用 ForEach,我得到:协议类型 'MyProtokoll' 的值不能符合 'Hashable';只有 struct/enum/class 类型可以符合协议 .
如果我尝试让 MyProtocol 符合 Hashable 我得到: Protocol 'MyProtokoll' can only be used as a generic约束,因为它有 Self 或关联类型要求.
如何存档?
struct PTest: View {
@State var allMyStruct : [MyStruct] =
[ MyStruct(name: "Frank Mueller", myshortName: "fm", a_lot_of_other_sttributes: "bla"),
MyStruct(name: "Tom Smith", myshortName: "ts", a_lot_of_other_sttributes: "bla")
]
var body: some View {
VStack {
Text("Hello, world!")
ListOfMyStruct(elements: allMyStruct)
}
.frame(width: 400, height: 400, alignment: .center)
.padding()
}
}
struct ListOfMyStruct: View {
var elements : [MyProtokoll]
var body: some View {
HStack {
Text("Elements of MyProtocol: ")
Text("\(elements[0].shortName() )")
Text("\(elements[1].shortName() )")
// ForEach(elements, id: \.self) { myProtocol in
// Text("\(myProtocol.shortName())")
// }
}
}
}
//protocol MyProtokoll : Identifiable, Hashable {
protocol MyProtokoll {
var id: String { get }
func shortName() -> String
}
struct MyStruct :MyProtokoll {
var id : String {myshortName}
var name : String
var myshortName :String
var a_lot_of_other_sttributes : String
func shortName() -> String {
myshortName
}
}
可以通过索引使用迭代。
这里是固定部分(用Xcode 12.4测试过)
HStack {
// Text("Elements of MyProtocol: ")
// Text("\(elements[0].shortName() )")
// Text("\(elements[1].shortName() )")
ForEach(elements.indices, id: \.self) { i in
Text("\(elements[i].shortName())")
}
}
另外,解决方案是明确指定 id
,例如
ForEach(elements, id: \.id) { myProtocol in
Text("\(myProtocol.shortName())")
}
我想编写一个 SwiftUI 视图 ListOfMyStruct,它在符合给定协议 MyProtocol.[=21 的元素数组上运行=]
该示例有效,但我当然需要 ForEach 遍历数组的元素(如在注释掉的行中尝试的那样)。
使用 ForEach,我得到:协议类型 'MyProtokoll' 的值不能符合 'Hashable';只有 struct/enum/class 类型可以符合协议 .
如果我尝试让 MyProtocol 符合 Hashable 我得到: Protocol 'MyProtokoll' can only be used as a generic约束,因为它有 Self 或关联类型要求.
如何存档?
struct PTest: View {
@State var allMyStruct : [MyStruct] =
[ MyStruct(name: "Frank Mueller", myshortName: "fm", a_lot_of_other_sttributes: "bla"),
MyStruct(name: "Tom Smith", myshortName: "ts", a_lot_of_other_sttributes: "bla")
]
var body: some View {
VStack {
Text("Hello, world!")
ListOfMyStruct(elements: allMyStruct)
}
.frame(width: 400, height: 400, alignment: .center)
.padding()
}
}
struct ListOfMyStruct: View {
var elements : [MyProtokoll]
var body: some View {
HStack {
Text("Elements of MyProtocol: ")
Text("\(elements[0].shortName() )")
Text("\(elements[1].shortName() )")
// ForEach(elements, id: \.self) { myProtocol in
// Text("\(myProtocol.shortName())")
// }
}
}
}
//protocol MyProtokoll : Identifiable, Hashable {
protocol MyProtokoll {
var id: String { get }
func shortName() -> String
}
struct MyStruct :MyProtokoll {
var id : String {myshortName}
var name : String
var myshortName :String
var a_lot_of_other_sttributes : String
func shortName() -> String {
myshortName
}
}
可以通过索引使用迭代。
这里是固定部分(用Xcode 12.4测试过)
HStack {
// Text("Elements of MyProtocol: ")
// Text("\(elements[0].shortName() )")
// Text("\(elements[1].shortName() )")
ForEach(elements.indices, id: \.self) { i in
Text("\(elements[i].shortName())")
}
}
另外,解决方案是明确指定 id
,例如
ForEach(elements, id: \.id) { myProtocol in
Text("\(myProtocol.shortName())")
}