SwiftUI 协议作为类型:值不能符合“可识别”;只有结构/枚举/类类型可以符合协议

时间:2021-01-04 01:21:07

标签: swift swiftui swift-protocols

希望大家度过了一个愉快的假期。新年快乐!

所以,我使用协议作为类型:

protocol Protocol_A_Or_B { }
protocol ProtocolA: Protocol_A_Or_B, Identifiable, Hashable, CaseIterable { }
protocol ProtocolB: Protocol_A_Or_B, Identifiable, Hashable, CaseIterable { }

我从协议中删除了所有变量和函数,因为我认为它们与我面临的问题无关。

我创建了几个实现 ProtocolAProtocolB 的枚举。另外,我有一个“主枚举”,它有一个方法返回一个实现 ProtocolAProtocolB 的对象数组,为此,ProtocolAProtocolB 有一个“父”协议 (Protocol_A_Or_B)。

enum MainEnum_: Int, Identifiable, Hashable, CaseIterable {
    case One
    case Two
    case Three
    case Four
    case Five
    
    var id:Int { rawValue }
    
    var aOrBEnumArray: [Protocol_A_Or_B] {
        switch self {
        case .One:
            return EnumTypeA.allCases
        case .Two:
            return EnumTypeB.allCases
        default:
            return []
        }
    }
}

enum EnumTypeA: Int, ProtocolA {
    case Option1
    case Option2
    case Option3
    
    var id:Int { rawValue } 
}

enum EnumTypeB: Int, ProtocolB {
    case OptionA
    case OptionB
    case OptionC
    
    var id:Int { rawValue }
}

到目前为止,一切都很好。但是当我尝试发送实现 Protocol_A_Or_B 的对象数组时,我收到以下错误:

Value of protocol type 'Protocol_A_Or_B' cannot conform to 'Hashable'; only struct/enum/class types can conform to protocols

Value of protocol type 'Protocol_A_Or_B' cannot conform to 'Identifiable'; only struct/enum/class types can conform to protocols

这是我使用对象的 View 的模拟。我在代码行 TheContentView2(data: arrayEnumTypeA)TheContentView2(data: arrayEnumTypeB) 中看到错误。

struct EnumProtocolsTest: View {
    var arrayEnumTypeA: [Protocol_A_Or_B] = MainEnum_.One.aOrBEnumArray
    var arrayEnumTypeB: [Protocol_A_Or_B] = MainEnum_.Two.aOrBEnumArray
    
    var body: some View {
        VStack {
            ScrollView(.horizontal, showsIndicators: false) {
                HStack {
                    TheContentView2(data: MainEnum_.allCases) { item in
                        Text("\(item.name)")
                            .padding()
                            .background(Color.purple)
                    }
                }
            }
            
            ScrollView(.horizontal, showsIndicators: false) {
                HStack {
                    TheContentView2(data: arrayEnumTypeA) { item in
                        Text("\(item.id)")
                            .padding()
                            .background(Color.purple)
                    }
                }
            }
            
            ScrollView(.horizontal, showsIndicators: false) {
                HStack {
                    TheContentView2(data: arrayEnumTypeB) { item in
                        Text("\(item.id)")
                            .padding()
                            .background(Color.purple)
                    }
                }
            }
        }
    }
}

因此,据我了解,我认为该错误与我拥有的自定义视图 (TheContentView2) 有关,因为它必须接收实现 Identifiable 和 {{1 }}。

Hashable

我无法将我的协议添加为该视图定义/签名的一部分,但如果它不正确,那么我可以请求更改。

希望你能帮助我。我不知道如何发送实现父协议 (struct TheContentView2<Data: RandomAccessCollection, ElementView: View>: View where Data.Element: Identifiable, Data.Element: Hashable { var data: Data var itemView: (Data.Element) -> ElementView var body: some View { ForEach(data) { item in itemView(item) .padding() .background(Color.purple) } } } ) 的对象数组。不确定协议定义或自定义视图的签名中缺少什么。

谢谢!!!

0 个答案:

没有答案