小编tbt*_*tbt的帖子

任何可识别的都不能符合“可识别”

更新:添加关于 Hashable 的相同错误


我创建了一个Identifiable合规的协议和合规的结构。然后,当我创建列表并在 中引用它时ForEach,我收到错误(关于)Type 'any TestProtocol' cannot conform to 'Identifiable'我收到相同的错误。Hashable

我应该如何修复这个程序?

如果我写ForEach(list, id: \.id) ,它可以工作,但我认为遵守可识别性是没有意义的。

import SwiftUI

protocol TestProtocol: Identifiable, Hashable {
    var id: UUID { get set }
    var name: String { get set }
    
    func greeting() -> String
    static func == (lhs: Self, rhs: Self) -> Bool
}

extension TestProtocol {
    static func == (lhs: Self, rhs: Self) -> Bool {
        return lhs.id == rhs.id
    }
}

struct Person: …
Run Code Online (Sandbox Code Playgroud)

swift swiftui swift5.7

10
推荐指数
1
解决办法
3165
查看次数

标签 统计

swift ×1

swift5.7 ×1

swiftui ×1