SwiftUI Swift - 类型不符合协议“Equatable”

问题描述 投票:0回答:1
import SwiftUI

struct MemeModel: Codable {
    var memeid: Int
    var title: String
    var pic: String
}

struct DataForGalleryShow: Hashable {
    var galleryMemes: [MemeModel]
    var category: String
}

struct NavStack: View {
   
    @State private var path = NavigationPath()
    
    var body: some View {
        NavigationStack {
            ZStack {
                Text("main")
            }
            .navigationDestination(for: DataForGalleryShow.self){ selection in
                GalleryShow(path: self.$path,
                    galleryMemes: selection.galleryMemes,
                    category: selection.category)
            }
        }
    }
}

struct GalleryShow: View {

    @Binding var path: NavigationPath
    var galleryMemes: [MemeModel]
    var category: String
    
    var body: some View {
        ZStack(){
            Text("bla")
        }
    }
}

我收到错误:

Type 'DataForGalleryShow' does not conform to protocol 'Equatable'

当我使用“修复”时,会出现以下情况:

struct DataForGalleryShow: Hashable {
    static func == (lhs: DataForGalleryShow, rhs: DataForGalleryShow) -> Bool {
        <#code#>
    }
    
    var galleryMemes: [MemeModel]
    var category: String
}

我不知道该怎么办,请帮忙。

我不会改变

galleryMemes
中的任何内容,只是传递这些数据而不稍后编辑它,但是对于其他任务,
MemeModel
仍然需要是
codable
,只是不在
DataForGalleryShow
中,这里可以将其转换为可散列如果当一切顺利时就有可能

ios swift swiftui
1个回答
0
投票

您需要做的就是使

MemeModel
符合
Hashable
协议。编译器将为您完成剩下的工作:

struct MemeModel: Codable, Hashable {
    let memeid: Int
    let title: String
    let pic: String
}
© www.soinside.com 2019 - 2024. All rights reserved.