我已經做了 8 個多小時,一遍又一遍地重構代碼,但我無法讓它作業,我認為這是:
ForEach(allWalletsData.walletList) { walletKey in
WalletStackView(user: user, walletAddress: walletKey)
}
它抱怨說:value of type 'String' has no member 'walletList'
如果我嘗試另一種組合,例如:
ForEach(allWalletsData.walletList) { walletKey in
WalletStackView(user: user, walletAddress: walletKey)
}
它說: referencing initializer 'init(_:content:)' on 'ForEach' requires that 'String' conform to 'Identifiable'
資料來自發布者:
@Published var allWalletsData: AllWalletsData = AllWalletsData(walletList: [], walletsData: [:], combinedWalletTokensQuotesFormatted: "")
在我的模型中,我已經用 1000 種不同的方式撰寫了它,但仍然......不起作用。
其中的資訊不能是靜態的,當我重繪 API 時,資料會發生變化。
這是我嘗試過的模型:
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
}
}
我在這一點上已經筋疲力盡,試圖找出發生了什么,有人知道發生了什么嗎?
編輯:
這是模型的第二部分,其中包含 ForEach 中使用的資料:
struct WalletInformation: Decodable, Encodable {
init(wallet: Wallet, chainsInfo: [ChainReCalculatedData], allChainsTotalBalanceValue: String) {
self.wallet = wallet
self.chainsInfo = chainsInfo
self.combinedChainsTokensQuotesFormatted = allChainsTotalBalanceValue
}
let wallet: Wallet
let chainsInfo: [ChainReCalculatedData]
// Combined chain balances
let combinedChainsTokensQuotesFormatted: String
}
extension WalletInformation: Hashable, Identifiable {
var id: String { wallet.address }
public func hash(into hasher: inout Hasher) {
hasher.combine(wallet)
hasher.combine(chainsInfo)
hasher.combine(combinedChainsTokensQuotesFormatted)
}
public static func ==(lhs: WalletInformation, rhs: WalletInformation) -> Bool {
if lhs.wallet != rhs.wallet {
return false
}
if lhs.chainsInfo != rhs.chainsInfo {
return false
}
if lhs.combinedChainsTokensQuotesFormatted != rhs.combinedChainsTokensQuotesFormatted {
return false
}
return true
}
}
uj5u.com熱心網友回復:
而不是制作AllWalletsData Identifiable,您需要制作您收藏的各個專案,Identifiable或者Hashable,或提供一個 ID。
您的收藏是allWalletsData.walletList- 根據您的模型,這是 type [String]。因此,一種選擇是:
ForEach(allWalletsData.walletList, id: \.self) { walletKey in
WalletStackView(user: user, walletAddress: walletKey)
}
這告訴SwiftUI使用String的id每個專案(這看起來像你試圖通過做來完成var id: [String] { walletList }。
請注意,如果walletAddress不包含真正獨特的元素,這(使用id: \.self)可能會導致不可預知的結果。另一條路要走的是為你的錢包定義一個模型,它有真正唯一的 id(比如 a UUID)。
更新,基于評論:
聽起來不像嘗試存盤資料的單獨的鍵和字典陣列,您應該只擁有一個模型陣列。就像是:
struct WalletInformation : Identifiable, Codable {
var id = //...
//other properties
}
struct AllWalletsData: Decodable, Encodable {
var walletList : [WalletInformation]
}
//and your ForEach:
ForEach(allWalletsData.walletList) { wallet in
WalletStackView(user: user, wallet: wallet)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/405326.html
標籤:
上一篇:HTML加載,但腳本不運行
