使协议符合Swift中的Hashable

发布于 2025-01-22 10:17:19 字数 850 浏览 1 评论 0原文

我正在尝试循环通过具有不同类型的练习的数组。为此,我声明了一个可以通过数组保存的协议。我想将Hashable协议添加到我的Anyerecise协议中。但是我会收到以下错误:协议'any ercercise'作为一种类型不能符合“ hashable”

我尝试了以下内容:

protocol AnyExercise {
var id: UUID {get}
var name: String {get}
var description: String {get}
func hash(into hasher: inout Hasher)
}

typealias Exercise = AnyExercise & Identifiable & Hashable

extension Break: Exercise {}
extension Pause: Exercise {}
extension Run: Exercise {}
extension Sprint: Exercise {}

示例结构实现:

struct Sprint {
var id = UUID()
var name: String
var description: String

init() {
    self.name = "Sprint"
    self.description = "Sprint"
}

func hash(into hasher: inout Hasher) {
    hasher.combine(id)
    hasher.combine(name)
    hasher.combine(description)
}
}

任何想法如何解决这个问题?还是在数组中保持多种不同类型?谢谢。

I am trying to loop through an array which holds different types of exercises. In order to do so, I declared a protocol AnyExercise which can be hold by an array. I would like to add the Hashable protocol to my AnyExercise protocol. But I am getting the following error: Protocol 'AnyExercise' as a type cannot conform to 'Hashable'

I tried the following:

protocol AnyExercise {
var id: UUID {get}
var name: String {get}
var description: String {get}
func hash(into hasher: inout Hasher)
}

typealias Exercise = AnyExercise & Identifiable & Hashable

extension Break: Exercise {}
extension Pause: Exercise {}
extension Run: Exercise {}
extension Sprint: Exercise {}

Example struct implementation:

struct Sprint {
var id = UUID()
var name: String
var description: String

init() {
    self.name = "Sprint"
    self.description = "Sprint"
}

func hash(into hasher: inout Hasher) {
    hasher.combine(id)
    hasher.combine(name)
    hasher.combine(description)
}
}

Any ideas how to solve this? Or hold multiple different types in an array? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等待圉鍢 2025-01-29 10:17:19

尽管以下内容不会使协议any codecise hashable,但它确实让您拥有
具有不同类型的数组,符合您的any cockise协议,并且在Swiftui中可用,
使用id:\ .id,如示例代码:

protocol AnyExercise {
    var id: UUID {get}
    var name: String {get}
    var description: String {get}
}

typealias Exercise = AnyExercise & Identifiable

extension Sprint: Exercise {}
extension Run: Exercise {}

struct Run {
    var id = UUID()
    var name: String
    var description: String
    
    init() {
        self.name = "Run"
        self.description = "Run"
    }
}

struct Sprint {
    var id = UUID()
    var name: String
    var description: String
    
    init() {
        self.name = "Sprint"
        self.description = "Sprint"
    }
}

struct ContentView: View {
    @State var results: [AnyExercise] = [Sprint(), Sprint(), Run()]
    
    var body: some View {
        ForEach(results, id: \.id) { item in
            Text(item.name)
        }
    }
}

although the following does not make the protocol AnyExercise Hashable, it does let you have
an array with different types conforming to your AnyExercise protocol and usable in SwiftUI,
using theid: \.id , as in the example code:

protocol AnyExercise {
    var id: UUID {get}
    var name: String {get}
    var description: String {get}
}

typealias Exercise = AnyExercise & Identifiable

extension Sprint: Exercise {}
extension Run: Exercise {}

struct Run {
    var id = UUID()
    var name: String
    var description: String
    
    init() {
        self.name = "Run"
        self.description = "Run"
    }
}

struct Sprint {
    var id = UUID()
    var name: String
    var description: String
    
    init() {
        self.name = "Sprint"
        self.description = "Sprint"
    }
}

struct ContentView: View {
    @State var results: [AnyExercise] = [Sprint(), Sprint(), Run()]
    
    var body: some View {
        ForEach(results, id: \.id) { item in
            Text(item.name)
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文