简体   繁体   English

Model 是可散列和可识别的,但仍然不能使用 ForEach

[英]Model is hashable and Identifiable but still can't use ForEach

I've been at it for more than 8h refactoring the code over and over again and I can't get it to work, I have this in my view:我已经做了 8 个多小时,一遍又一遍地重构代码,但我无法让它工作,我认为这是:

ForEach(allWalletsData.walletList) { walletKey in
    WalletStackView(user: user, walletAddress: walletKey)
}

and it complains saying: value of type 'String' has no member 'walletList' if I try another combination like:它抱怨说:如果我尝试另一种组合,则value of type 'String' has no member 'walletList'

ForEach(allWalletsData.walletList) { walletKey in
    WalletStackView(user: user, walletAddress: walletKey)
}

it says: referencing initializer 'init(_:content:)' on 'ForEach' requires that 'String' conform to 'Identifiable'它说: referencing initializer 'init(_:content:)' on 'ForEach' requires that 'String' conform to 'Identifiable'

In my model I've wrote it 1000 different ways and still... doesn't work.在我的 model 中,我已经用 1000 种不同的方式编写了它,但仍然......不起作用。

The information in it cannot be static, as I refresh the API, the data will change.里面的信息不能是static,因为我刷新API,数据会发生变化。

This is the model I've tried:这是我试过的 model:

struct AllWalletsData: Decodable, Encodable {
    init(walletList: [String], walletsData: OrderedDictionary<String, WalletInformation>, combinedWalletTokensQuotesFormatted: String) {
        self.walletList = walletList
        self.walletsData = walletsData
        self.combinedWalletTokensQuotesFormatted = combinedWalletTokensQuotesFormatted
    }

    let walletList: [String]
    let walletsData: OrderedDictionary<String, WalletInformation>
    let combinedWalletTokensQuotesFormatted: String
}

extension AllWalletsData: Hashable, Identifiable {
    var id: [String] { walletList }

    func hash(into hasher: inout Hasher) {
        hasher.combine(combinedWalletTokensQuotesFormatted)
    }

    static func ==(lhs: AllWalletsData, rhs: AllWalletsData) -> Bool {
        if lhs.combinedWalletTokensQuotesFormatted != rhs.combinedWalletTokensQuotesFormatted {
            return false
        }
        return true
    }
}

I've burned out at this point trying to find out what's happening, does anyone know what's going on?我在这一点上已经筋疲力尽,试图找出发生了什么,有人知道发生了什么吗?

Instead of making AllWalletsData Identifiable , you need to make the individual items of your collection Identifiable or Hashable , or provide an ID.您需要将集合中的各个项目设置为IdentifiableHashable ,或者提供一个 ID,而不是使AllWalletsData Identifiable

Your collection is allWalletsData.walletList -- according to your model, this is of type [String] .您的收藏是allWalletsData.walletList - 根据您的 model,这是[String]类型。 So, one option is:因此,一种选择是:

ForEach(allWalletsData.walletList, id: \.self) { walletKey in
    WalletStackView(user: user, walletAddress: walletKey)
}

That tells SwiftUI to use the String as the id for each item (which looks like what you were trying to accomplish by doing var id: [String] { walletList } .这告诉 SwiftUI 使用String作为每个项目的id (这看起来就像你试图通过执行var id: [String] { walletList }来完成的。

Beware that if walletAddress doesn't contain truly unique elements, this (using id: \.self ) can lead to unpredictable results.请注意,如果walletAddress不包含真正唯一的元素,这(使用id: \.self )可能会导致不可预知的结果。 The other route to go would be defining a model for your wallet that had truly unique ids (like a UUID ).通往 go 的另一条途径是为您的钱包定义一个具有真正唯一 ID(如UUID )的 model。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM