0
import SwiftUI

class aViewModel: ObservableObject {
    @Published var abc: Set<(aa:String,bb:String,cc:Double,dd:Int)> = []  // Type '(aa: String, bb: String, cc: Double, dd: Int)' does not conform to protocol 'Hashable'
    
    func fetchData() async throws {
        // Url Stuff
    }
}

struct ContentView: View {
    @StateObject var ViewModel = aViewModel()
    
    var body: some View {
        ...
            .task {
                do {
                    try await ViewModel.fetchData()
                } catch {
                // Show Alert
            }
        }
        ForEach($ViewModel.abc,id: \.self) { e in
            //Using for stuff..
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

I tried not putting <(aa:String,bb:String,cc:Double,dd:Int)> but this causes to be any set. Because any is not Hashable it causes error. I can't even change to array because set fixes an bug. If i try to solve the bug with array it causes big performance drops. What do I need to do in this situation?

  • Radioactive: note that the answer that you just accepted was created using ChatGPT. I'd be very wary of the veracity of the answer as this tool has known issues with reliability. The answers it provides "sound" correct, but they are often wrong. Also, the poster, who has been posting nothing but AI-generated answers should not be rewarded for this behavior, as they are not actually answering questions, the information that they provide you can get yourself from ChatGPT, should you desire, and their actions do nothing to help the site. – Hovercraft Full Of Eels Jun 30 '23 at 17:57
  • Please don't fight the framework. Apple says in [Swift Language Guide: The Basics](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/thebasics): *Tuples are useful for simple groups of related values. They’re not suited to the creation of complex data structures. If your data structure is likely to be more complex, model it as a class or structure, rather than as a tuple. For more information, see [Structures and Classes](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/classesandstructures).* – vadian Jun 30 '23 at 18:35
  • While @Vadian's guidance is good, the problem you have here is nothing to do with `@Published` but due to you trying to use a data structure that isn't inherently mashable in a `Set` (as the error message tells you). – flanker Jun 30 '23 at 20:17
  • grrr. *hashable* Thanks, autocorrect, – flanker Jun 30 '23 at 23:38

0 Answers0