gpt4 book ai didi

swift - "Duplicate keys of type ... were found in a Dictionary "什么时候没有字典?

转载 作者:行者123 更新时间:2023-12-04 13:10:45 24 4
gpt4 key购买 nike

我对 Swift 很陌生,我刚刚遇到了一个我找不到解决方案的错误。我目前正在开发一个游戏(Boggle,为了好奇),我想更新算法找到的单词列表。
我创建了一个结构来保存每个单词及其得分:

struct ScoredWord: Comparable, Identifiable{
let word: String
var points: Int = 0
let id: UUID

init(word: String){
self.id = UUID()
self.word = word
self.points = self.defineScore(word: word)
}

static func < (lhs: ScoredWord, rhs: ScoredWord) -> Bool {}

static func == (lhs: ScoredWord, rhs: ScoredWord) -> Bool {}

func hash(into hasher: inout Hasher) {

private func defineScore(word: String) -> Int{}
(我删除了 func 的内容,因为它对你没用)
算法完成后,我有一个简单的循环,为找到的每个单词创建一个结构并将其存储在@Published 的数组中以供显示
let foundWords = solver.findValidWords()

for found in foundWords {
wordList.append(ScoredWord(word: found))
}
在我看来,数组是这样使用的:
 List(wordListViewModel.wordList, id: \.self) { // 1 Word list
Text( $0.word )
Spacer()
Text("\( $0.points )")
}
当我运行所有这些时,我得到的错误是:
Fatal error: Duplicate keys of type 'ScoredWord' were found in a Dictionary. 
This usually means either that the type violates Hashable's requirements, or
that members of such a dictionary were mutated after insertion.
我找到了 this post关于相同的错误,其中评论指出错误将来自列表显示不够快并且 ID 混淆,但没有关于如何修复它......
任何的想法?

最佳答案

我认为您没有完全符合 Hashable Protocol
关注 func hash(into hasher: inout Hasher)你缺少的功能

关于swift - "Duplicate keys of type ... were found in a Dictionary "什么时候没有字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65466863/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com