import SwiftUI
class DataFetcherViewModel: ObservableObject {
@Published var someValues: Set<(value1: String, value2: String, value3: Double, value4: Int)> = [] // 🛑 Type '(value1: String, value2: String, value3: Double, value4: Int)' does not conform to protocol 'Hashable'
func fetchData() async {
try? await Task.sleep(nanoseconds: 2_000_000_000)
someValues.insert((value1: String, value2: String, value3: Double, value4: Int))
}
}
struct ContentView: View {
@StateObject var viewModel = DataFetcherViewModel()
var body: some View {
Button {
Task(operation: viewModel.fetchData)
} label: {
Text("Click Me")
}
ForEach(viewModel.someValues, id: \.self) { value in
VStack {
Text(value.value1)
Text(value.value2)
Text(String(value.value3))
Text(String(value.value4))
}.background {
RoundedRectangle(cornerRadius: 20)
}
}
}
}
我尝试不指定通用名称,但这会导致它成为
Set<Any>
。因为 Any
不是 Hashable
,所以也会导致错误。我无法将其更改为数组,因为它会导致性能下降。这种情况我需要做什么?